规则如下:
SELECT
clientid as cid,
now_timestamp('millisecond') as now_ts_ms,
nth(6, split(topic, '/')) as attr_old,
['INT32', 'FLOAT'] as data_types,
['level', 'voltage'] as measurements,
jq('.level', payload) as lvls,
jq('.voltage', payload) as vols,
FROM
"dev/+/r/battery"
调试输入数据:
{"ts": 1695885691, "level": 61, "voltage": 3.968182}
调试输出结果:
attr_old | battery |
---|---|
cid | c_emqx |
data_types | |
0 | INT32 |
1 | FLOAT |
lvls | |
0 | 61 |
measurements | |
0 | level |
1 | voltage |
now_ts_ms | 1695885932319 |
vols | |
0 | 3.968182 |
实际环境发布数据:
mosquitto_pub -t dev/500000000001/r/battery -i 500000000001 -m '{"level": 61, "voltage": 3.968182}'
实际环境错误日志:
2023-09-28T07:26:47.730354+00:00 [error] msg: action_failed, mfa: emqx_rule_runtime:handle_action/4, line: 343, peername: 172.17.0.1:41910, clientid: 500000000001, action: #{func => console,mod => emqx_rule_actions}, exception: error, reason: function_clause, stacktrace: [{emqx_rule_runtime,do_handle_action,[<<"doorbell-events">>,#{func => console,mod => emqx_rule_actions},#{<<"attr_old">> => <<"battery">>,<<"cid">> => <<"500000000001">>,<<"data_types">> => [<<"INT32">>,<<"FLOAT">>],<<"lvls">> => "=",<<"measurements">> => [<<"level">>,<<"voltage">>],<<"now_ts_ms">> => 1695886007728,<<"vols">> => [3.968182]},#{clientid => <<"500000000001">>,event => 'message.publish',flags => #{dup => false,retain => false},headers => #{peerhost => {172,17,0,1},properties => #{},proto_ver => 4,protocol => mqtt,username => undefined},id => <<"000606663B11FA61E8B1010078570002">>,metadata => #{rule_id => <<"doorbell-events">>},node => 'emqx@172.17.0.16',payload => <<"{\"level\": 61, \"voltage\": 3.968182}">>,peerhost => <<"172.17.0.1">>,pub_props => #{'User-Property' => #{}},publish_received_at => 1695886007728,qos => 0,timestamp => 1695886007728,topic => <<"dev/500000000001/r/battery">>,username => undefined}],[{file,"emqx_rule_runtime.erl"},{line,347}]},{emqx_rule_runtime,handle_action,4,[{file,"emqx_rule_runtime.erl"},{line,326}]},{emqx_rule_runtime,'-handle_action_list/4-lc$^0/1-0-',4,[{file,"emqx_rule_runtime.erl"},{line,321}]},{emqx_rule_runtime,do_apply_rule,3,[{file,"emqx_rule_runtime.erl"},{line,180}]},{emqx_rule_runtime,apply_rule,3,[{file,"emqx_rule_runtime.erl"},{line,72}]},{emqx_rule_runtime,apply_rule_discard_result,3,[{file,"emqx_rule_runtime.erl"},{line,65}]},{emqx_rule_runtime,apply_rules,3,[{file,"emqx_rule_runtime.erl"},{line,61}]},{emqx_rule_events,on_message_publish,2,[{file,"emqx_rule_events.erl"},{line,142}]},{emqx_hooks,safe_execute,2,[{file,"emqx_hooks.erl"},{line,200}]},{emqx_hooks,do_run_fold,3,[{file,"emqx_hooks.erl"},{line,180}]},{emqx_broker,publish,1,[{file,"emqx_broker.erl"},{line,219}]},{emqx_channel,do_publish,3,[{file,"emqx_channel.erl"},{line,714}]},{emqx_connection,with_channel,3,[{file,"emqx_connection.erl"},{line,840}]},{emqx_connection,process_msg,2,[{file,"emqx_connection.erl"},{line,493}]},{emqx_connection,process_msg,2,[{file,"emqx_connection.erl"},{line,499}]},{emqx_connection,handle_recv,3,[{file,"emqx_connection.erl"},{line,455}]},{proc_lib,wake_up,3,[{file,"proc_lib.erl"},{line,250}]}]
lvls成了一个等于号
<<"lvls">> => "="