I want to use Node-RED for automating my home. And I use RFXtrx433XL as 433MHz sensor gateway. The data from the sensors is received just fine, so that works. But when getting those values into Node-RED (for the purpose of e.g. turning a panel oven on/off when evaluating the room temperature), I was adviced to use MQTT. That is, push the values received on 433MHz to MQTT topics, and then read those topics in Node-RED to carry out automations. But how can I have a trigger on the received values on 433MHz that pushes the received value to MQTT?
How is this related to home assistant?
Use an automation with state trigger , with only entity_id, so it will trigger on every change.
Then you can use the Trigger state object to get the value and publish it to a mqtt topic.
Thanks. I want the automation to trigger on all values, and basically push all values received to MQTT. From the examples I’ve found, it seems I have to set a range (from
/to
or above
/below
) when using state
or numeric_state
. I’ve tried the following:
alias: Kitchen Temperature
trigger:
platform: event
event_type: signal_received
event_data: {'entity_id': 'sensor.kitchen_temperature'}
action:
service: mqtt.publish
data:
topic: "home/notification"
payload_template: "{{ states.sensor.kitchen_temperature }}"
This does fire on every temperature received, but in MQTTBox I see that the payload contains all of the information from the sensor, not just the temperature.
<template state sensor.kitchen_temperature=21.5; Temperature=21.5, Humidity=45,
Humidity status=comfort, Humidity status numeric=1, Battery numeric=9, Rssi numeric=6,
unit_of_measurement=°C, friendly_name=Kitchen Temperature @ 2018-11-03T11:44:15.011387+01:00>
So I gather that I either have to limit the payload template in some way, or I shouldn’t trigger on events and rather use a different method.
When you omit the state in the state trigger, it triggers on evey change.
alias: Kitchen Temperature
trigger:
platform: state
entity_id: sensor.kitchen_temperature
action:
service: mqtt.publish
data:
topic: "home/notification"
payload_template: "{{ trigger.to_state.state }}"
With the payload you can play in Dev Tools/Templates.
For example:
{% set value = '<template state sensor.kitchen_temperature=21.5; Temperature=21.5, Humidity=45,
Humidity status=comfort, Humidity status numeric=1, Battery numeric=9, Rssi numeric=6,
unit_of_measurement=°C, friendly_name=Kitchen Temperature @ 2018-11-03T11:44:15.011387+01:00>' %}
{{ value.split(';')[1].split(',')[0].split('=')[1] }}
Returns: 21.5
EDIT: I think you don’t need to split the value.
Just use "{{ states.sensor.kitchen_temperature.state }}"
or "{{ trigger.to_state.state }}"
Ah, thanks a lot. The final state
in the payload template did the trick.
- alias: Kitchen Temperature
trigger:
platform: event
event_type: signal_received
event_data: {'entity_id': 'sensor.kitchen_temperature'}
action:
service: mqtt.publish
data:
topic: "home/notification"
payload_template: "{{ states.sensor.kitchen_temperature.state }}"
As for numeric_state, it seems it’s mandatory to set either above or below. The YAML check fails if I don’t set it.
I said the ‘state’ trigger, not ‘numeric_state’ !
I stand corrected: state
worked as well.
Any thoughts on triggering on an event vs triggering on a state change, performance-wise?
Sorry, no!
Did you investigate using node-red-contrib-home-assistant?
Rather than using automations to publish/push the sensor values, you use a node that already knows how to talk to Home Assistant (via its REST API). The “event-state” node will receive state-changes from the sensors.
If your sensors are publishing a JSON payload, it’s very easy to parse it in Node-Red (no need to filter it in Home Assistant).
There are many video tutorials available. Here’s one from The Hookup.
EDIT
There’s another version of node-red-contrib-home-assistant that communicates with Home Assistant via a websocket (vs HTTP). It’s called node-red-contrib-home-assistant-websocket and its advantages are described in this thread.
Yes, I started out with using the Home Assistant nodes directly in Node-RED. But when asking a question about extracting the value/state of the sensors when receiving the events (https://discourse.nodered.org/t/switching-on-off-oven-using-temperature-in-home-assistant/4486), I was advised to use MQTT rather than HA events in Node-RED.
It seemed like a logical choice, given that I eventually want to implement InfluxDB and Grafana into the mix (and who knows what else… ).
My problem is that I lack the experience to know the best approach.
Option 1:
- Receive 433 MHz sensor values in HA.
- Read HA events in Node-RED, and somehow extract values/states.
- Regulate and drive outputs (wall-plugs in my case) from Node-RED using HA nodes.
Option 2:
- Receive 433 MHz sensor values in HA.
- Convert sensor values to MQTT in HA.
- Read MQTT sensor values in Node-RED.
- Regulate and drive outputs from Node-RED using HA nodes.
Option 3:
- Receive 433 MHz sensor values in HA.
- Convert sensor values to MQTT in HA.
- Read MQTT sensor values in Node-RED.
- Regulate and push output states to MQTT.
- Trigger on output MQTT topics in HA, and drive outputs from HA.
Option 4:
- Receive 433 MHz sensor values directly in Node-RED using RFXtrx433/rfxcom palette (was unable to get this installed for some reason).
- Regulate and drive outputs from Node-RED using HA nodes.
And so on and so forth…
I’m leaning towards option 2 or 3, since some of the stuff in my home has ready-made plugins for Home Assistant but not for Node-RED.
If you mean the advice given to you by the first person to reply, they qualified their response with:
… you are far better off on the HA forum and asking them for the how to on this as your data is originating from there and there are a lot of knowledgeable people on that forum re MQTT interactions
Kudos to that individual for adding that advice because I don’t think they’re aware of node-red-contrib-home-assistant.
node-red-contrib-home-assistant is for interfacing Home Assistant with Node-Red. It makes it very easy to consume data from Home Assistant to build flows in Node-Red. It’s also bi-directional; you can get data from Home Assistant and also send data to Home Assistant.
Option 1 is your best choice if you also plan to use the sensor values within Home Assistant. If not and you only wish to use them within Node-Red then Option 4 is the logical choice. Options 2 and 3 needlessly create more steps in the process of getting from A to B.
Read HA events in Node-RED, and somehow extract values/states.
If the sensor payload is a JSON object, the “somehow” is super easy. Let’s assume this is what the sensor sends: {“temperature”: 24, “humidity”: 45}
- msg.payload contains everything sent by the sensor (the entire JSON object).
- msg.payload.temperature contains 24.
- msg.payload.humidity contains 45.
How well would that fit into expanding the system? Are there any downsides with not converting the values to MQTT topics, in regards to e.g. adding InfluxDB/Grafana, offloading services onto a different RPi, or other “that would be a cool thing to do” cases that are likely to come in foreseeable future?
… offloading services onto a different RPi, or other “that would be a cool thing to do” cases that are likely to come in foreseeable future?
If you keep moving the goalposts then eventually there will be something you can’t do with Node-Red. However, for everything you’ve asked for so far, this is the right direction.