Get RGB Light to previous state after color change for a couple of seconds

Hello,

I put a litte photo resistor in my Alexa Echo dot so when the light ring lights up an esp8266 detects that and triggers a binary_sensor. With that information I power on/off an LED Strip (Because I sometimes I don’t see the ring on the echo dot and it’s kind of a cool effect).

So the ring is lightning up for around 3 seconds or something and then it should go back to the state it was before it was turned on by the Alexa binary_sensor (for example some color fading etc.). How do I achieve this?

Here’s a screenshot of the flow with the compelte .msg on the right.

Thanks a lot

Chris

I would be interested in this if you or anyone else sorted this out.

Here’s a node that will hold a message in queue

What might be easier is just storing the value in a variable within the Node-Red flow and using that

https://nodered.org/docs/writing-functions#storing-data

Here’s a quick sample, it’s untested but might give you the general idea.

[{"id":"c1ae8902.bd1b38","type":"change","z":"5b64ba49.24ef54","name":"","rules":[{"t":"set","p":"prevState.state","pt":"msg","to":"data.state","tot":"msg"},{"t":"set","p":"prevState.color","pt":"msg","to":"rgb_color","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":896,"y":160,"wires":[["2858c35e.15be9c"]]}]

Using a change node to save the values you want to restore.

[{"id":"7077615d.f895d","type":"function","z":"5b64ba49.24ef54","name":"Set Previous State","func":"msg.payload = {\n    domain: msg.payload.domain,\n    service: `turn_${msg.prevState.state}`,\n    data: {\n        entity_id: msg.payload.data.entity_id,\n        rgb_color: msg.prevState.rgb_color\n    }\n};\n\nreturn msg;","outputs":1,"noerr":0,"x":938,"y":224,"wires":[["dd75df4c.18ad5"]]}]

Changing those saved values into a format that the call-service node can use.

2 Likes

Just one small correction – line 6 should be:

rgb_color: msg.prevState.color

1 Like

Hello good people. I am trying to do something very similar: When i walk into my bedroom a motion detector should trigger a brightness/color change (go to full brighness, white light) and if no motion is detected, go back to the previous state of the light after 2min.

Since i am using Ikea Tradfri lights that cannot process two commands at a time, there is a subflow that splits the message containing the info into bits and sends it to the light with some delays in between. You can see how the expected message going into the subflow should look like in the “bedroom”-change node.

Now i constructed the below Flow, also adding brightness as a reset-value. However the reset is not working. Any ideas on how i should proceed? I suspect the way Kermit’s change & function nodes construct de msg does not play well with my subflow for the Tradfri lights. However i am by far too new to Node-Red to understand exactly what is wrong…

[{"id":"20b81b5b.682884","type":"subflow","name":"Tradfri light color","info":"","category":"","in":[{"x":40,"y":100,"wires":[{"id":"cfd6ffb7.b8e4e"}]}],"out":[],"env":[],"color":"#DDAA99"},{"id":"9f086959.46c608","type":"api-call-service","z":"20b81b5b.682884","name":"Turn on lights","server":"b805682b.1077a8","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"","data":"{\"transition\":1}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":420,"y":60,"wires":[[]]},{"id":"5c5cbc36.782294","type":"api-call-service","z":"20b81b5b.682884","name":"Set color","server":"b805682b.1077a8","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"","data":"{\"transition\":2}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":540,"y":120,"wires":[[]]},{"id":"2c870fe0.7aef","type":"delay","z":"20b81b5b.682884","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":400,"y":120,"wires":[["5c5cbc36.782294"]]},{"id":"cfd6ffb7.b8e4e","type":"function","z":"20b81b5b.682884","name":"Split payload","func":"br = {\n    payload: {\n        data: {\n            entity_id: msg.payload.entity_id,\n            brightness_pct: msg.payload.brightness_pct\n        }\n    }\n}\n\ncolor = {\n    payload: {\n        data: {\n            entity_id: msg.payload.entity_id,\n            rgb_color: msg.payload.rgb_color\n        }\n    }\n}\n\nwarmth = {\n    payload: {\n        data: {\n            entity_id: msg.payload.entity_id,\n            color_temp: msg.payload.color_temp\n        }\n    }\n}\n\nreturn [br, color, warmth]\n","outputs":3,"noerr":0,"x":170,"y":100,"wires":[["9f086959.46c608"],["2c870fe0.7aef"],[]]},{"id":"189edc3e.c7cb14","type":"server-state-changed","z":"9a07763e.6bef38","name":"bedroom motion","server":"b805682b.1077a8","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.presence_7","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":200,"y":1320,"wires":[["2310bce9.f4eb34"]]},{"id":"2640d8cc.2bd6c8","type":"change","z":"9a07763e.6bef38","name":"","rules":[{"t":"set","p":"prevState.state","pt":"msg","to":"data.state","tot":"msg"},{"t":"set","p":"prevState.color","pt":"msg","to":"rgb_color","tot":"str"},{"t":"set","p":"prevState.brightness","pt":"msg","to":"brightness_pct","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":1320,"wires":[["b9cd1d6e.68f9f","1774f3c8.ef5cfc"]]},{"id":"d06a06d7.ab9878","type":"function","z":"9a07763e.6bef38","name":"Set Previous State","func":"msg.payload = {\n    domain: msg.payload.domain,\n    service: `turn_${msg.prevState.state}`,\n    data: {\n        entity_id: msg.payload.data.entity_id,\n        rgb_color: msg.prevState.color,\n        brightness_pct:prevState.brightness\n    }\n};\n\nreturn msg;","outputs":1,"noerr":0,"x":1130,"y":1380,"wires":[["fea1a2d6.cb4c2"]]},{"id":"2310bce9.f4eb34","type":"api-current-state","z":"9a07763e.6bef38","name":"","server":"b805682b.1077a8","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.bedroom_mood","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":460,"y":1320,"wires":[["2640d8cc.2bd6c8"]]},{"id":"b9cd1d6e.68f9f","type":"change","z":"9a07763e.6bef38","name":"bedroom","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"entity_id\":\"light.bedroom_mood\",\"rgb_color\":[255,255,200],\"brightness_pct\":100}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":940,"y":1280,"wires":[["d37b1c92.c44a5"]]},{"id":"1774f3c8.ef5cfc","type":"delay","z":"9a07763e.6bef38","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":940,"y":1380,"wires":[["d06a06d7.ab9878"]]},{"id":"fea1a2d6.cb4c2","type":"subflow:20b81b5b.682884","z":"9a07763e.6bef38","name":"Tradfri light color","env":[],"x":1350,"y":1380,"wires":[]},{"id":"d37b1c92.c44a5","type":"subflow:20b81b5b.682884","z":"9a07763e.6bef38","name":"Tradfri light color","env":[],"x":1350,"y":1280,"wires":[]},{"id":"b805682b.1077a8","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

An easy solution might be to use the new[ish] scene.create service. This will allow you to capture the state of the lights in question and then restore them easily without any of the other stuff.

1 Like

Yeah I thought about that too, however i try to keep as much of my stuff in one place - and since i really am much more comfortable with node-red than HA/.YAML i also built all my scenes in NR. I do keep track of the last activated scene in NR by a global variable, so i could also re-invoke that scene automatically after the sensor-timer runs out, however that would always mean the whole scene gets reset, so i would loose all the adjustments i made since first setting the scene (like e.g. manually brightening the bedroom lights). So i really need to find a way to request the state (brightness-pct, rgb_color / kelvin) of a light, safe that state and when the timer runs out, send it back to the light in exactly this msg.payload format: (example-values, if it was a temp-light “rgb_color” would get replaced by “klevin”)

{
    "entity_id": "bedroom_nightstand",
    "rgb_color": [
        255,
        255,
        150
    ],
    "brightness_pct": 80
}

Setting variables for every single lamp in every single possible state to be later re-invoked is not an option - i would end up with some 100+ variables to keep track of.

Any ideas? @Kermit seems to have a woking approach, however i can’t really figure out how to recreate / adapt it to my needs.

Just for the next one coming around here:

This can be archived using the scene.create service, as mentioned by @code-in-progress - thanks for that.

My flow looks like this:

the timestamp node is used for debugging as it can simply be used as a trigger.

Store

image

{
   "scene_id":"before",
   "snapshot_entities":[
       "light.deckenlampe_68d521fe_level_light_color_on_off"
   ]
}

Turn Light Green

image

{
   "color_name":"green",
   "brightness":255,
   "flash":"short"
}

Restore

image

2 Likes

The ability to create scenes and restore them works really well for single lights. However, how would you do it, if you want to capture the state of ALL lights (rgb, brightness, etc) to be recalled?

You can pass in as many lights entities as you like to create the scene.

Do you have to hardcode them individually or is there a way to use the output/payload of the get entities node? I’ve tried the latter unsuccessfully, but I’m not good with Json or JS.