Newbie: Multiple states to trigger

I am looking to set a workflow where all entities need to be in a set state (no motion = off, specific light = off) and then wait 10 minutes before performing the action.

I have it working with one state using the events:state node but can’t get it working when both entities are compliant.

Here is my code:

[{"id":"d81d43a35568ab4d","type":"tab","label":"Desk Lights","disabled":false,"info":"","env":[]},{"id":"306cd4c818ed7e64","type":"server-state-changed","z":"d81d43a35568ab4d","name":"Under Desk Motion Detection OFF","server":"26a44118.302a0e","version":5,"outputs":2,"exposeAsEntityConfig":"","entityId":["binary_sensor.qingping_motion_light_sensor_t_motion_sensor","light.standing_desk_lstrip"],"entityIdType":"list","outputInitially":false,"stateType":"str","ifState":"off","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":true,"for":"10","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":140,"y":380,"wires":[["d200f0d45ccf1cf0"],[]]},{"id":"d200f0d45ccf1cf0","type":"api-call-service","z":"d81d43a35568ab4d","name":"","server":"26a44118.302a0e","version":5,"debugenabled":false,"domain":"switch","service":"turn_off","areaId":[],"deviceId":[],"entityId":["switch.wall_light","switch.tp_link_power_strip_1d6e_left_monitor_light","switch.tp_link_power_strip_1d6e_right_monitor_light"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":520,"y":360,"wires":[[]]},{"id":"78a4497d96f23c4f","type":"server-state-changed","z":"d81d43a35568ab4d","name":"Under Desk Motion Detection ON","server":"26a44118.302a0e","version":5,"outputs":2,"exposeAsEntityConfig":"","entityId":"binary_sensor.qingping_motion_light_sensor_t_motion_sensor","entityIdType":"exact","outputInitially":false,"stateType":"str","ifState":"on","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":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":150,"y":480,"wires":[["8e9127f7b0a3b117"],[]]},{"id":"8e9127f7b0a3b117","type":"api-call-service","z":"d81d43a35568ab4d","name":"","server":"26a44118.302a0e","version":5,"debugenabled":false,"domain":"switch","service":"turn_on","areaId":[],"deviceId":[],"entityId":["switch.wall_light","switch.tp_link_power_strip_1d6e_left_monitor_light","switch.tp_link_power_strip_1d6e_right_monitor_light"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":440,"y":480,"wires":[[]]},{"id":"26a44118.302a0e","type":"server","name":"Home Assistant","addon":true}]

Thanks in advance!

The easiest way to deal with this is to create a mixed group entity in HA. This entity will require all entities to be off in order to have a state of off. Use that entity in the event state with the time condition.

Unfortunately this sensor cannot be created in the UI, it has to be defined in group.yaml. You will find it in your config folder. Add the following: (change the entities and name)

mixed:
  name: mixedGroup
  entities:
    - light.upstairs
    - switch.kitchen_counter

In NR you would do you would do it like this:


The trigger nodes are just there to start the flow.
The check state nodes is the one checking the states and by using the “if state” settings direct the flow to a success or failure check.

If you just want to wait 10 minutes after the checks is done, then a delay node before the debug node here will handle it.
If you want to wait 10 minutes, but abort if any states are changed away from the desired one, then both the triggers and the check state nodes should be set to that delay in the “For” field.

Another way to keep it all in node red, we can use jsonata. In an event state use the list option and set the if state fields both to JSONata. In the and statement add all entities like so:

($entities('light.upstairs').state = "off") and ($entities('light.upstairs_2').state = 'off')

Thank you @WallyR and @Mikefila !

@Mikefila I am trying the JSONata approach, I notice on your screenshot you have “Output on Connect” - that is isn’t checked by default. Should I check that, and what does it do?

That will trigger the node when it is first deployed or the contents change. It’s used for testing. You can uncheck it once everything is working.

I want to explain why I first suggested a group. When dealing with consumables, like a light bulb, I lean towards groups. Hard coding entities that may change over time can be cumbersome to maintain.

You would need to change every instance to the new entity should something change. Using a group, the entity can be changed in the group without having to edit any flows.

Thanks a lot of the explanation!

One other condition I want to add is when my Mac screen is off. Using the companion app, it looks like I can get that from the “Screen Off” attribute of the sensor.active - I am just not sure how to query for that when its an attribute vs state.

How do I add that to the JSONata?

Should be ($entities('sensor.active').attributes.screen_off = true). The mobile app usually adds the name of the device to the sensor. So check the sensor name.

Thanks so much for your help so far!

I am struggling to get the Mac sensor to work. I settled on the screensaver attribute as it has been more reliable as it is a desktop Mac:

(
    $entities('binary_sensor.mac_active').attributes.screensaver = true
)

I set up a separate events:state node to help debug as below

[{"id":"c897aa6589b1b602","type":"server-state-changed","z":"d81d43a35568ab4d","name":"Mac Screensaver On","server":"26a44118.302a0e","version":5,"outputs":2,"exposeAsEntityConfig":"","entityId":"binary_sensor.mac_active","entityIdType":"exact","outputInitially":false,"stateType":"str","ifState":"(\t    $entities('binary_sensor.mac_active').attributes.screensaver = true\t)","ifStateType":"jsonata","ifStateOperator":"jsonata","outputOnlyOnStateChange":true,"for":"10","forType":"num","forUnits":"seconds","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":110,"y":200,"wires":[[],[]]},{"id":"26a44118.302a0e","type":"server","name":"Home Assistant","addon":true}]

but it keeps showing a red/white square (never a green solid square) when it is “on” or “off” which I believe is then holding up the group node I want to include it in.

Do I need to enter the JSONata logic differently, as it is an attribute?

Have you tried "on" with the quotes instead of true?

Tried with “on” and now it just sticks to on (red/white square) regardless if screensaver is on or off.

Can you post the debug output for 'binary_sensor.mac_active'

Here’s the output from the false branch node (Screensaver off)

3/13/2024, 8:12:06 PMnode: Screensaver off
binary_sensor.mac_active : msg.payload : string[3]
"off"
3/13/2024, 8:12:09 PMnode: Screensaver offbinary_sensor.mac_active : msg.payload : string[2]
"on"

and this is using:

(
    $entities('binary_sensor.mac_active').attributes.screensaver = "on"
)

I don’t see the attribute for screensaver. I expected this format where the attributes are defined. Are you sure it is not just

$entities('binary_sensor.mac_active').state = "off"

I’m looking for something like this.

Set the entity in a current state node and post the debug.

[{"id":"2a3de46120e39c8e","type":"api-current-state","z":"60f2d2277843c698","name":"","server":"6b1110b5.183a4","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"media_player.nowhere","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":590,"y":2620,"wires":[["1e2e6826354e97f1"]]},{"id":"2a5d383dd1367854","type":"inject","z":"60f2d2277843c698","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":210,"y":2620,"wires":[["2a3de46120e39c8e"]]},{"id":"1e2e6826354e97f1","type":"debug","z":"60f2d2277843c698","name":"debug 32","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":860,"y":2620,"wires":[]},{"id":"6b1110b5.183a4","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":false,"heartbeat":false,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

Sorry, how do I create the debug. Do I create a current state note and then have two debug notes running off it like this?

Nothing is posting in the debug for this setup:
image

and I am using the same JSONata logic

(
    $entities('binary_sensor.mac_active').attributes.screensaver = true
)

I am following the attributes as defined here:

You just make an inject node that goes to a current state node with just the sensor in the entity field.
Then you take the output and make it go to a debug node that you make sure to set the output field to the complete msg object.
Click the inject node when the computer is in the different states and copy-paste the debug output here.
The debug output will have a little icon to the right when holding you mouse over the line. This icon will copy the entire output and can then be pasted in a post here.

Thanks WallyR and Mikefila

Hopefully this is what is needed. It looks like I should be using the ‘Screen Off’ attribute instead of Screensaver. How do I code that in JSONata when there is a space in the attribute as it complains there is a syntax error when using this:

(
    $entities('binary_sensor.mmdt_active').attributes.screen off = true
)

Mac Active

/14/2024, 12:13:04 PMnode: debug 1msg : Object
object
_msgid: "2684807e722958ab"
payload: "on"
topic: ""
data: object
entity_id: "binary_sensor.mac_active"
state: "on"
attributes: object
Fast User Switched: false
Idle: false
Locked: false
Screen Off: false
Screensaver: false
Sleeping: false
Terminating: false
icon: "mdi:monitor"
friendly_name: "Mac Active"
last_changed: "2024-03-14T16:12:26.639Z"
last_updated: "2024-03-14T16:12:26.639Z"
context: object
id: "01HRYVRZ4F0632H26QHC0681CA"
parent_id: null
user_id: null
timeSinceChangedMs: 38265

Mac Not Active

3/14/2024, 12:13:54 PMnode: debug 1
msg : Object
object
_msgid: "914b4f75f0635875"
payload: "off"
topic: ""
data: object
entity_id: "binary_sensor.mac_active"
state: "off"
attributes: object
Fast User Switched: false
Idle: false
Locked: true
Screen Off: true
Screensaver: false
Sleeping: false
Terminating: false
icon: "mdi:monitor-off"
friendly_name: "Mac Active"
last_changed: "2024-03-14T16:13:47.337028+00:00"
last_updated: "2024-03-14T16:13:47.463Z"
context: object
id: "01HRYVVE27QT28VNTDZEJDJ29J"
parent_id: null
user_id: null
timeSinceChangedMs: 7174

It looks like you need to use the Screen Off value instead of the Screensaver.

Screen off is probably when the screen is turned off electrically.
Screensaver is when the screen is still on, but some program is showing things on the screen or just a black screen to prevent burn-in.

Thanks - how do I write that in JSONata to add to my group when there is a space in the attribute name?

The capital and white space was the problem

$entities('binary_sensor.mac_active').attributes["Screen off"] = true