Node Red, current state node

I’m fault tracing me flow where I try to control the lights.
I have following flow and the switch is off from the beginning.


I inject “on” for test purpose into my flow, the current state node is supposed to check the state of the switched if it is already on. Pause flow otherwise turn on light.
The problem I have is with the current state node, it change the payload of the message and are not behaving as I should expect.

What I’m I doing wrong?

1 Like

I believe you are actually switching the state with the Check State node. Remove the “on” from the If State field and see if it works.

The If State field is used to change the state of an entity under certain conditions. If you only want to check the state then the field should be blank.

Although looking at the debug that may not be the case. Try it and see if it works.

That is not the way it works.
The current state node fetches the state from HA, you do not need to feed it the state like you are trying to do here.

Also, your check seems odd. Your node label is “lights on ?”, but you check if it is NOT on. When you hover the first output of the node, it will say ‘true’. This will falsely make you conclude (given on the name you gave to the node), that the lights are on.

Devasator,
Is this a better way of doing it?


image

Why not put “on” in the if state of the “current state node” ? Then it will generate 2 outputs: True/False. Rest seems OK to me

Thanks for clearing that up.

If you don’t want it to change the message payload then change the ‘state location’ option, you have it set to write the state into the payload. You can write to another message variable if you need both the state and the original payload later in your flow. Or you can just discard it, if you just use it with the ‘if state’.

1 Like

Devastator,
Now I updated my flow, I tried to change it according to your comments.


image
Is it how you meant ?

I’m curious what’s triggering these flows?

I have made a flow that calculate the sun elevation + a time window.
The output from that flow is input to this flow

Not sure the complexity of your elevation check but there’s a node, eztimer, that allows you to set a time or sun based event with the option for randomization. Using this node might help simplify your flows.

Also as far as I understand it you really don’t need to check the state of the switch before sending a turn on/off service call. There shouldn’t be any ill effects.

[{"id":"920d4a4f.1de438","type":"eztimer","z":"d7029474.cf9948","name":"","autoname":"dusk - sunrise","suspended":false,"sendEventsOnSuspend":false,"timerType":"1","startupMessage":true,"ontype":"1","ontimesun":"dusk","ontimetod":"17:00","onproperty":"payload","onvaluetype":"str","onvalue":"on","onoffset":"30","onrandomoffset":true,"offtype":"1","offtimesun":"sunrise","offtimetod":"dusk","offduration":0,"offproperty":"payload","offvaluetype":"str","offvalue":"off","offoffset":"30","offrandomoffset":true,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":true,"sun":true,"x":144,"y":96,"wires":[["34be314f.a68f5e","9688988d.105638"]]},{"id":"34be314f.a68f5e","type":"api-call-service","z":"d7029474.cf9948","name":"Facade Lights","service_domain":"homeassistant","service":"turn_{{payload}}","data":"{\"entity_id\":\"switch.facade_lights\"}","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":336,"y":96,"wires":[[]]},{"id":"9688988d.105638","type":"rbe","z":"d7029474.cf9948","name":"change ?","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":316,"y":144,"wires":[["2156a5f3.b504ca"]]},{"id":"2156a5f3.b504ca","type":"debug","z":"d7029474.cf9948","name":"Send to Pushbullet","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":522,"y":144,"wires":[]}]

Or if you still want to check the state of the switch before acting upon it.

[{"id":"83e528b1.bc8818","type":"eztimer","z":"d7029474.cf9948","name":"","autoname":"dusk - sunrise","suspended":false,"sendEventsOnSuspend":false,"timerType":"1","startupMessage":true,"ontype":"1","ontimesun":"dusk","ontimetod":"17:00","onproperty":"payload","onvaluetype":"str","onvalue":"on","onoffset":"30","onrandomoffset":true,"offtype":"1","offtimesun":"sunrise","offtimetod":"dusk","offduration":0,"offproperty":"payload","offvaluetype":"str","offvalue":"off","offoffset":"30","offrandomoffset":true,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":true,"sun":true,"x":144,"y":208,"wires":[["fee494dd.ffde18"]]},{"id":"fee494dd.ffde18","type":"api-current-state","z":"d7029474.cf9948","name":"Facade Light","version":1,"outputs":2,"halt_if":"payload","halt_if_type":"msg","halt_if_compare":"is_not","override_topic":false,"entity_id":"switch.facade_light","state_type":"str","state_location":"","override_payload":"none","entity_location":"","override_data":"none","blockInputOverrides":false,"x":326,"y":208,"wires":[["e2d8a826.f316d8","139807d8.2d45e8"],[]]},{"id":"e2d8a826.f316d8","type":"api-call-service","z":"d7029474.cf9948","name":"Facade Lights","service_domain":"homeassistant","service":"turn_{{payload}}","data":"{\"entity_id\":\"switch.facade_lights\"}","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":512,"y":208,"wires":[[]]},{"id":"139807d8.2d45e8","type":"rbe","z":"d7029474.cf9948","name":"change ?","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":492,"y":256,"wires":[["50d6cf3.681113"]]},{"id":"50d6cf3.681113","type":"debug","z":"d7029474.cf9948","name":"Send to Pushbullet","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":682,"y":256,"wires":[]}]

edit: This flow should allow you to replace four flows into one.

Could you share how you would pass the previous payload through and keep the current payload if I wanted to use both? I figured out how to pass through the payload with global.payload.

Hi
This how I do it in a function block.

const c_different = context.global.get("g_statusNoPrev") !== context.get("n_statusNo");
context.global.set("g_statusNoPrev", context.get("n_statusNo"));


if (c_different) {                                  //value changed!
    xxxxxxxx
    
}
else {
    xxxxxxxxx
}

This was my solution.
More info here: https://community.home-assistant.io/t/override-payload-not-working-in-current-state-node/57763/16

1 Like

@Kermit your example helped a lot, but I could not set up: domain: homeassistant; service: turn_{{payload}}

is there any plug in necessary?

You’ll need to install the integration as well as the add-on/docker container.