"Save" a complete state and give it back

Hi guys,

I want to make a switch for a colorloop, since there is no simple “turn on and off” for the effect in home assistant. What I want to to do is this:

  1. If I turn on my switch, I want to get the complete current state of 2 of my lights (since I use them just together).
  2. After that, I want these 2 lights to start the color loop.
  3. If I turn off the switch, I want to set the 2 lights back to their old state.

How can I realize that? I know how to call a service, but don’t know how to call the complete payload, which was saved in 1…

Can anyone help me here pls?

You can save state in “flow. variable” and then call it back if it is switched off.

What do you mean? To just rename the payload? I know, but I don’t know how to call it back … maybe you have an example?

Btw.: i don’t know if I expressed myself clearly, I want to save the complete state (brightness, color, and all other attributes) and call this complete state (with all attributes) back. Not just on or off…

[
    {
        "id": "d52d6b3e.64f158",
        "type": "debug",
        "z": "416aa994.4ed0a8",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 1310,
        "y": 4180,
        "wires": []
    },
    {
        "id": "b800f482.f9644",
        "type": "api-current-state",
        "z": "416aa994.4ed0a8",
        "name": "",
        "server": "3c4e982f.072f98",
        "version": 1,
        "outputs": 1,
        "halt_if": "",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "override_topic": false,
        "entity_id": "light.desk",
        "state_type": "str",
        "state_location": "payload",
        "override_payload": "msg",
        "entity_location": "data",
        "override_data": "msg",
        "blockInputOverrides": false,
        "x": 1070,
        "y": 4120,
        "wires": [
            [
                "2bf5f354.a399b4",
                "138f5da9.07a192"
            ]
        ]
    },
    {
        "id": "7d327404.504334",
        "type": "switch",
        "z": "416aa994.4ed0a8",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "on",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "off",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 830,
        "y": 4140,
        "wires": [
            [
                "b800f482.f9644"
            ],
            [
                "4537ef74.a2b348"
            ]
        ]
    },
    {
        "id": "b9649304.617668",
        "type": "server-state-changed",
        "z": "416aa994.4ed0a8",
        "name": "",
        "server": "3c4e982f.072f98",
        "version": 1,
        "exposeToHomeAssistant": false,
        "haConfig": [
            {
                "property": "name",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            }
        ],
        "entityidfilter": "switch.2min",
        "entityidfiltertype": "exact",
        "outputinitially": true,
        "state_type": "str",
        "haltifstate": "",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "outputs": 1,
        "output_only_on_state_change": true,
        "x": 510,
        "y": 4120,
        "wires": [
            [
                "7d327404.504334"
            ]
        ]
    },
    {
        "id": "2bf5f354.a399b4",
        "type": "change",
        "z": "416aa994.4ed0a8",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "oldstate1",
                "pt": "flow",
                "to": "data.attributes",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1310,
        "y": 4120,
        "wires": [
            []
        ]
    },
    {
        "id": "4537ef74.a2b348",
        "type": "change",
        "z": "416aa994.4ed0a8",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "data.attributes",
                "pt": "msg",
                "to": "oldstate1",
                "tot": "flow"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1070,
        "y": 4180,
        "wires": [
            [
                "d52d6b3e.64f158"
            ]
        ]
    },
    {
        "id": "138f5da9.07a192",
        "type": "debug",
        "z": "416aa994.4ed0a8",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 1310,
        "y": 4080,
        "wires": []
    },
    {
        "id": "3c4e982f.072f98",
        "type": "server",
        "z": "",
        "name": "Home Assistant1111",
        "legacy": true
    }
]

Thank you for your help my friend. Still my problem is: how do I call the state? I understand how to save an output (like your example), but how to call this output then? Do I have to call the service “light.turn_on” with templating the output, or can I somehow just call the complete output? I don’t like to call the “light.turn_on” service, because what if the light was off before the effect turned on?

Still need help. @n1k5y just made me an offer to help me for 5$ via PN, which I rejected. Thats the first time I saw this here…

1 Like

Have a nice day

[{"id":"b800f482.f9644","type":"api-current-state","z":"416aa994.4ed0a8","name":"","server":"3c4e982f.072f98","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.desk","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":1030,"y":4120,"wires":[["2bf5f354.a399b4","9b91ce09.fea9a8","aa52a5dd.b757e8"]]},{"id":"7d327404.504334","type":"switch","z":"416aa994.4ed0a8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":830,"y":4140,"wires":[["b800f482.f9644"],["7ffbc569.c584d4"]]},{"id":"b9649304.617668","type":"server-state-changed","z":"416aa994.4ed0a8","name":"","server":"3c4e982f.072f98","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"switch.2min","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":640,"y":4140,"wires":[["7d327404.504334"]]},{"id":"2bf5f354.a399b4","type":"change","z":"416aa994.4ed0a8","name":"","rules":[{"t":"set","p":"oldstate1","pt":"flow","to":"data.attributes","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1310,"y":4120,"wires":[[]]},{"id":"9b91ce09.fea9a8","type":"change","z":"416aa994.4ed0a8","name":"","rules":[{"t":"set","p":"oldstate","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1300,"y":4080,"wires":[[]]},{"id":"7ffbc569.c584d4","type":"switch","z":"416aa994.4ed0a8","name":"","property":"oldstate","propertyType":"flow","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":970,"y":4180,"wires":[["879839d8.6c415"],["a2c36dcf.43ffc"]]},{"id":"a2c36dcf.43ffc","type":"api-call-service","z":"416aa994.4ed0a8","name":"","server":"3c4e982f.072f98","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.desk","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1150,"y":4240,"wires":[[]]},{"id":"283af034.72a898","type":"api-call-service","z":"416aa994.4ed0a8","name":"","server":"3c4e982f.072f98","version":1,"debugenabled":true,"service_domain":"light","service":"turn_on","entityId":"light.desk","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":true,"x":1310,"y":4180,"wires":[[]]},{"id":"6b004e52.aab6c","type":"api-call-service","z":"416aa994.4ed0a8","name":"","server":"3c4e982f.072f98","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.desk","data":"{\"brightness\":255,\"transition\":5,\"rgb_color\":[255,0,0]}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1490,"y":4040,"wires":[[]]},{"id":"aa52a5dd.b757e8","type":"delay","z":"416aa994.4ed0a8","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1290,"y":4040,"wires":[["6b004e52.aab6c"]]},{"id":"879839d8.6c415","type":"function","z":"416aa994.4ed0a8","name":"","func":"var a = flow.get(\"oldstate1\");\nmsg.payload={\n    \"data\":{\n        \"rgb_color\":a.rgb_color,\n        \"brightness\":a.brightness\n    }\n}\nreturn msg;","outputs":1,"noerr":0,"x":1130,"y":4180,"wires":[["283af034.72a898"]]},{"id":"3c4e982f.072f98","type":"server","z":"","name":"Home Assistant1111","legacy":true}]

TBH I don’t want your help anymore, after this offer :). And yet you don’t understand what my problem is. I know how to get information of a payload, I know how to call a service with this information, I know all this stuff. I just want to know if it is possible to give an entity a whole payload without calling a service.
Thx anyway for your input.

okayy… I suggest to start from beginning…

With homeassistant’s scene features, this is very easy now and you don’t need to write functions in node-red. please read it here.
when you turn on the switch, create a scene, just before starting the color loop and use that scene to bring the lights back to initial states by turning the switch off.

here is a flow I use, you can use as an example:

[{"id":"27641109.a537ee","type":"trigger-state","z":"6e3966a6.5190b8","name":"start musical statues","server":"e447d17a.16a64","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"input_boolean.musical_statues","entityidfiltertype":"exact","debugenabled":false,"constraints":[],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":120,"y":1020,"wires":[["63cd996c.1b29a8"],[]]},{"id":"63cd996c.1b29a8","type":"switch","z":"6e3966a6.5190b8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":310,"y":1020,"wires":[["6e800dd2.fe6024","8b92e7f8.2c8678"],["a4a1ee5d.0f616"]]},{"id":"6e800dd2.fe6024","type":"api-call-service","z":"6e3966a6.5190b8","name":"save the scene","server":"e447d17a.16a64","version":1,"debugenabled":true,"service_domain":"scene","service":"create","entityId":"","data":"{\"scene_id\":\"lights_before_game\",\"snapshot_entities\":\"light.living_room_middle\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":520,"y":980,"wires":[[]]},{"id":"a4a1ee5d.0f616","type":"api-call-service","z":"6e3966a6.5190b8","name":"turn lights to normal","server":"e447d17a.16a64","version":1,"debugenabled":true,"service_domain":"scene","service":"turn_on","entityId":"scene.lights_before_game","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":530,"y":1060,"wires":[[]]},{"id":"8b92e7f8.2c8678","type":"delay","z":"6e3966a6.5190b8","name":"","pauseType":"delay","timeout":"10","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":510,"y":1020,"wires":[["cd16102d.bcd2f"]]},{"id":"cd16102d.bcd2f","type":"api-call-service","z":"6e3966a6.5190b8","name":"start lights","server":"e447d17a.16a64","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.living_room_middle, light.on_off_plug_4","data":"{\"brightness\":\"255\",\"effect\":\"colorloop\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":710,"y":1020,"wires":[[]]},{"id":"e447d17a.16a64","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false}]
1 Like

Thank you my friend! This is exactly what I was looking for!
Still there is a “issue” here, here is an example:

If my lights are turned on, with a brightness of 50. If I turn them off, and then again on, the brightness will be 50 again. But if I use your method (wich I really like) and the lights were turned off (with the brightness of 50, like before), and I turn on and off the input_boolean, the brightness after turning the lamp manually on will be 255 (definition of scene) and not the 50…
This is confusing :D… I hope you understand :slight_smile:

is it what is happening when you use the scenes?

before color loop: lights: on, brightness: 50
after color loop: lights:on, brightness: 255

I didn’t expect this outcome, after you apply the schene again, all the entities should go back to state when you record the scene. did you give a few milliseconds to save the scene before starting the color loop?

this part is little bit confusing, how are the lights are off with 50% brightness? if they were off when flip the input_boolean, after flipping the input_boolean off I expect them to be turned off again…

No, everything is working, if the light was turned on and then applying the color loop.
But if the lights were turned off, and I turn on the input_boolean (wich applies the brightness of 255 and color loop) and turn it off after that, this brightness will remain after turning the light on directly (without input_boolean).

the scene domain provides a ‘create’ service. So basically you can take a snapshot of entities and creates a scene on the fly, once you finish the loop you can reapply the scene.
This type of scenes don’t survive scene reloads or reboots, they are intended for this type of automation.

This can obviously be controlled from nodered