Current State

The Current State node has several opportunities to use JSONata. This example demonstrates all of these, using a node to fetch the current state of a climate air-conditioning unit.

The default output values for this node include setting msg.payload to the state (which for an A/C unit will be "off", "heat", "cool" or more, depending on the integration) as well as msg.data to the entire entity data object. This holds values for the last changed timestamp as well as attributes such as the friendly name and other integration dependent settings.

screenshot

Here are three examples, showing how to use JSONata to build output properties, to perform If-State conditional tests, and to provide calculated values for UI settings.

[{"id":"d0f1fad3bf96a090","type":"group","z":"776c027950fc8c3f","name":"Current State node - read the state of an entity","style":{"label":true,"color":"#000000"},"nodes":["6394700dfd39838f","8388eb36d3e25254","852c05cf8c7cae73","a3e69091339a457c","11fc322e343cbd9c","6ac52ef3e9010615","63fa3b7423417fe4","a29810da49f1efd4","4f8197db85d55fb5","fd6d41b2f67b85e0","36912490ae36c0db","f84772ea62e6e7ba","6244f42dd53b7e1f","bc6cd64ba0266d97"],"x":34,"y":499,"w":1012,"h":382},{"id":"6394700dfd39838f","type":"api-current-state","z":"776c027950fc8c3f","g":"d0f1fad3bf96a090","name":"","server":"","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"climate.bedroom_aircon","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"(\t    $att:=$entity().attributes;\t    $diff:=$att.temperature-$att.current_temperature;\t    ($diff>0 ? \"Heating\" : \"Cooling\") & \" by \" & $abs($diff) & \" degrees required\"\t)","valueType":"jsonata"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":430,"y":580,"wires":[["852c05cf8c7cae73"]]},{"id":"8388eb36d3e25254","type":"inject","z":"776c027950fc8c3f","g":"d0f1fad3bf96a090","name":"Manual trigger","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":150,"y":580,"wires":[["6394700dfd39838f"]]},{"id":"852c05cf8c7cae73","type":"debug","z":"776c027950fc8c3f","g":"d0f1fad3bf96a090","name":"Output Text","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":770,"y":580,"wires":[]},{"id":"a3e69091339a457c","type":"comment","z":"776c027950fc8c3f","g":"d0f1fad3bf96a090","name":"2a - JSONata builds output in current state node","info":"","x":240,"y":540,"wires":[]},{"id":"11fc322e343cbd9c","type":"api-current-state","z":"776c027950fc8c3f","g":"d0f1fad3bf96a090","name":"","server":"","version":3,"outputs":2,"halt_if":"$entities('sensor.time').state < \"17:00\" ? \"cool\" : \"off\"","halt_if_type":"jsonata","halt_if_compare":"is","entity_id":"climate.bedroom_aircon","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"}],"for":"$entity().state = \"off\" ? 0 : 2","forType":"jsonata","forUnits":"hours","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":430,"y":700,"wires":[["63fa3b7423417fe4"],["a29810da49f1efd4"]]},{"id":"6ac52ef3e9010615","type":"inject","z":"776c027950fc8c3f","g":"d0f1fad3bf96a090","name":"Manual trigger","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":150,"y":700,"wires":[["11fc322e343cbd9c"]]},{"id":"63fa3b7423417fe4","type":"debug","z":"776c027950fc8c3f","g":"d0f1fad3bf96a090","name":"Cooling for 2+ hours before 17:00, or Off","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":840,"y":680,"wires":[]},{"id":"a29810da49f1efd4","type":"debug","z":"776c027950fc8c3f","g":"d0f1fad3bf96a090","name":"otherwise","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":700,"y":740,"wires":[]},{"id":"4f8197db85d55fb5","type":"comment","z":"776c027950fc8c3f","g":"d0f1fad3bf96a090","name":"2b - JSONata builds conditional test value, and UI parameter","info":"","x":280,"y":660,"wires":[]},{"id":"fd6d41b2f67b85e0","type":"comment","z":"776c027950fc8c3f","g":"d0f1fad3bf96a090","name":"2c - JSONata builds conditional Boolean","info":"","x":220,"y":780,"wires":[]},{"id":"36912490ae36c0db","type":"api-current-state","z":"776c027950fc8c3f","g":"d0f1fad3bf96a090","name":"","server":"","version":3,"outputs":2,"halt_if":"(\t    $state:=$entity().state;\t    $time:=$entities('sensor.time').state;\t    ($time<=\"09:00\" or $time>=\"17:00\") and ($state=\"heat\" or $state=\"cool\")\t)","halt_if_type":"jsonata","halt_if_compare":"jsonata","entity_id":"climate.bedroom_aircon","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entity"}],"for":"$substringAfter($now(),\"T\")>\"10:00\" ? 20 : 10","forType":"jsonata","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":430,"y":820,"wires":[["6244f42dd53b7e1f"],["bc6cd64ba0266d97"]]},{"id":"f84772ea62e6e7ba","type":"inject","z":"776c027950fc8c3f","g":"d0f1fad3bf96a090","name":"Manual trigger","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":150,"y":820,"wires":[["36912490ae36c0db"]]},{"id":"6244f42dd53b7e1f","type":"debug","z":"776c027950fc8c3f","g":"d0f1fad3bf96a090","name":"On outside of office hours","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload.state","targetType":"msg","statusVal":"payload.state","statusType":"auto","x":850,"y":800,"wires":[]},{"id":"bc6cd64ba0266d97","type":"debug","z":"776c027950fc8c3f","g":"d0f1fad3bf96a090","name":"otherwise","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload.state","targetType":"msg","statusVal":"payload.state","statusType":"auto","x":680,"y":840,"wires":[]}]

Creating output properties

Example: Report the difference between room temperature and target temperature for an air-conditioning unit.

In the first example we are looking to retrieve the current state and attributes from an air-conditioning unit, and output a text message describing the difference between current and target temperature. The A/C unit current temperature (read only) and the A/C unit target temperature (can be set with a service call) are located in the entity attributes. These are both numbers, so it is very easy to calculate the difference.

Note: The default value msg.data is set only for the output message, and therefore msg.data.state would be used to obtain the entity state in nodes further along the flow, but $entity().state is used to obtain the entity state in JSONata expressions inside the node. When using JSONata to build an output message property, there is no requirement to retain the default output property settings for msg.data unless this is specifically required later in the flow.

(
    $att:=$entity().attributes;
    $diff:=$att.temperature-$att.current_temperature;
    ($diff>0 ? "Heating" : "Cooling") & " by " & $abs($diff) & " degrees required"
)

Providing a conditional test value (JSONata expression as a value)

Example: Output only where the A/C unit has been cooling for two hours and the local time is before 17:00, or when the unit is off.

The Current State node provides for a state value test opportunity, which if set can be used to direct the message output one of two ways, depending whether the entity state passes or fails the test. When the If State option is chosen with a conditional (not JSONata) the condition expects a value to test against. In this example, using JSONata on the right hand side will return the value of the evaluated expression ("cool" before 17:00 and "off" otherwise) to be tested against the current state value and using the chosen conditional operator.

screenshot

$entities('sensor.time').state < "17:00" ? "cool" : "off"

Generating UI field setting values

The Current State node also allows for a For, or duration of state test, and here JSONata is again being used to return either '0' when the state is "off" or '2' (hours) when the state is not "off". This complex arrangement will together test for the A/C unit being either immediately off after 17:00, or in cooling mode for at least two hours before 17:00.

$entity().state = "off" ? 0 : 2

Note that here the use of $entity().state returns the current state of the node's subject entity, and that $entities('sensor.time').state returns the state of another specified entity. The Time & Date Integrationopen in new window should be added to the configuration file, and then the time sensor will return the current local time. This is useful for local-time condition testing, whereas the time_utc sensor will return UTC-time which is more useful with Home Assistant timestamps.

Conditional test (JSONata expression as a Boolean result)

Example: Report the air-conditioning unit being on outside of office hours.

When the If State option is chosen with JSONata (not a conditional) then the right hand side must be a JSONata expression that returns a Boolean value of either true or false. When true the If State test is successful (the message is output from the top exit) when false the test fails (the message is output from the lower exit).

screenshot

In this example the JSONata expression is a code block, obtaining the node subject entity state and separate entity local time as in the previous example, but now combining both into one predicate expression. If the local time is outside 09:00 to 17:00 and the state is either heat or cool, then the expression will return true.

(
    $state:=$entity().state;
    $time:=$entities('sensor.time').state;
    ($time<="09:00" or $time>="17:00") and ($state="heat" or $state="cool")
)

JSONata used at this point can be extensive, with a combination of entity states or attributes and Boolean logic as required. The use of OR to combine various states can only be more easily achieved in this way.

Also see: