Sensor
Using the WebSocket nodes provides many ways to read events and entity states from Home Assistant into Node-RED. Sending data the other way requires the use of the Entity nodes.
There are several, but they all work in much the same way, and this example will use the Sensor node to create an entity sensor in Home Asisstant, and then to update the sensor state value as well as add an attribute value.
Here is an example, showing how to use JSONata to set state value, and attribute value settings for an entity-sensor node.
[{"id":"c14460fdc14a24a0","type":"group","z":"1cc7d2e94a4815fe","name":"Sensor node - set state and attribute values","style":{"label":true,"color":"#000000"},"nodes":["ba71e67aa0d06f02","4c7a952705afe4a0","e06208fe5410cfb5","23dfef724e706db3","a6d8e76c9ca8af97","fbbda709b5a50be7"],"x":214,"y":219,"w":912,"h":222},{"id":"ba71e67aa0d06f02","type":"inject","z":"1cc7d2e94a4815fe","g":"c14460fdc14a24a0","name":"Manual Trigger","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":340,"y":260,"wires":[["4c7a952705afe4a0"]]},{"id":"4c7a952705afe4a0","type":"ha-sensor","z":"1cc7d2e94a4815fe","g":"c14460fdc14a24a0","name":"How many lights are on","entityConfig":"02814df043c7111b","version":0,"state":"$entities().*[state = \"on\" and entity_id ~> /^light|^switch/] ~> $count()","stateType":"jsonata","attributes":[{"property":"lights_table","value":"$entities().*[state = \"on\" and entity_id ~> /^light|^switch/].{\"name\": attributes.friendly_name, \"lastChange\": last_changed}","valueType":"jsonata"},{"property":"time","value":"","valueType":"date"}],"inputOverride":"allow","outputProperties":[],"x":610,"y":260,"wires":[[]],"server":""},{"id":"e06208fe5410cfb5","type":"server-state-changed","z":"1cc7d2e94a4815fe","g":"c14460fdc14a24a0","name":"","server":"","version":5,"outputs":1,"exposeAsEntityConfig":"","entityId":"sensor.lights_on_count","entityIdType":"exact","outputInitially":false,"stateType":"num","ifState":"","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"table","propertyType":"msg","value":"$entity().attributes.lights_table","valueType":"jsonata"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"},{"property":"changes","propertyType":"msg","value":"(\t $old:=$prevEntity().attributes.lights_table;\t $new:=$entity().attributes.lights_table;\t {\"on\": $new[$not(name in $old.name)],\t \"off\": $old[$not(name in $new.name)]}\t)","valueType":"jsonata"}],"x":630,"y":340,"wires":[["23dfef724e706db3","a6d8e76c9ca8af97","fbbda709b5a50be7"]]},{"id":"23dfef724e706db3","type":"debug","z":"1cc7d2e94a4815fe","g":"c14460fdc14a24a0","name":"Count of lights on","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":990,"y":280,"wires":[]},{"id":"a6d8e76c9ca8af97","type":"debug","z":"1cc7d2e94a4815fe","g":"c14460fdc14a24a0","name":"Array table","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"table","targetType":"msg","statusVal":"payload","statusType":"auto","x":970,"y":340,"wires":[]},{"id":"fbbda709b5a50be7","type":"debug","z":"1cc7d2e94a4815fe","g":"c14460fdc14a24a0","name":"Changes","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"changes","targetType":"msg","statusVal":"changes","statusType":"auto","x":960,"y":400,"wires":[]},{"id":"02814df043c7111b","type":"ha-entity-config","server":"","deviceConfig":"","name":"SC LS On Count","version":"6","entityType":"sensor","haConfig":[{"property":"name","value":"LS On Count"},{"property":"icon","value":""},{"property":"entity_picture","value":""},{"property":"entity_category","value":""},{"property":"device_class","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""}],"resend":false,"debugEnabled":false}]
Setting the sensor state value
Example: Provide a sensor with the count of lights that are on.
Once a new entity has been registered with Home Assistant, the entity state value can be updated by passing a message with the new value. Commonly msg.payload is used, however it is possible to use a JSONata expression to generate the new state value.
$entities().*[state = "on" and entity_id ~> /^light|^switch/] ~> $count()
Here the $entities()
function is used to return all entities, and select those that have "light" or "switch" in the 'entity id', and have an "on" state. This will return an array of entities, which can be counted to obtain the total number of switches or lights that are on.
Setting an attribute value
Example: Provide a list of the lights that are on.
The entity state value can only be a JSON primitive value, such as a number or string, and objects and arrays can only be passed using the entity attributes.
$entities().*[state = "on" and entity_id ~> /^light|^switch/].{
"name": attributes.friendly_name,
"lastChange": last_changed}
The JSONata expression here first returns a similar array of lights and switches, but from this then builds an array of objects. Each object contains the friendly name and last changed timestamp for each light or switch. This array is set into an attribute value of the sensor entity.
Responding to state changes using JSONata
Example: Since I last looked, which lights have been turned on, or off?
In addition to the node above, the example also includes an Event: state node which responds to state changes in the above sensor update.
(
$old:=$prevEntity().attributes.lights_table;
$new:=$entity().attributes.lights_table;
{"on": $new[$not(name in $old.name)],
"off": $old[$not(name in $new.name)]
}
)
In an Event node, the $entity()
function returns the current or new entity details, and the $prevEntity()
function the previous or old entity details. The JSONata here picks out the sensor attribute and compares the array of lights & switches from before and after the state change. From this, an object with an array list of lights or switches that have been turned on, and those turned off, is provided.
Note: In this example the Sensor node is updated on demand. The state value of the associated Home Assistant entity, which is a count of the number of lights or switches that are on, will only be updated when the Sensor node is triggered. The Events: state node is responding to changes in the sensor node state value, which will only happen when the sensor node is updated, and not when a light or switch is turned on or off.
Also see: