Python_script to save and restore switches and lights

anyone got an example for using this in nodered ? i can’t seem to get it to store or restore when calling it via nodered.

[{"id":"356d075.6d9c7f8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"868cfaa8.aeefd8","type":"api-call-service","z":"356d075.6d9c7f8","name":"Store Kitchen LEDs","server":"e6482986.08f318","service_domain":"python_script","service":"light_store","data":"{   \"store_name\": \"flash_store\",  \"entity_id\": \"light.kitchen_led_1\" }","mergecontext":"","x":610,"y":220,"wires":[[]]},{"id":"977beba4.1bf438","type":"api-call-service","z":"356d075.6d9c7f8","name":"Restore Lights","server":"e6482986.08f318","service_domain":"python_script","service":"reload","data":"{   \"store_name\": \"flash_store\",  \"operation\": \"restore\" }","mergecontext":"","x":600,"y":280,"wires":[[]]}

Did you find a resolution for this?

Nope, tho I stopped trying shortly after posting this.
:tired_face:

I use this one I got from another thread to grab the variables of a Hue light and then restore them when I am done.

[{"id":"9a3534ea.0efa68","type":"tab","label":"Store & Restore Hue","disabled":false,"info":""},{"id":"71c52db0.fdcfa4","type":"inject","z":"9a3534ea.0efa68","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":340,"wires":[["6fe3f95e.85969"]]},{"id":"6fe3f95e.85969","type":"api-current-state","z":"9a3534ea.0efa68","name":"","server":"f65952bb.822ef","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"light.office_light","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":350,"y":340,"wires":[["b5ba0383.6c7b5"]]},{"id":"b5ba0383.6c7b5","type":"switch","z":"9a3534ea.0efa68","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":570,"y":340,"wires":[["f629912a.1ab62"],["8189d023.566ae8"]]},{"id":"f629912a.1ab62","type":"change","z":"9a3534ea.0efa68","name":"","rules":[{"t":"set","p":"lightState","pt":"flow","to":"{ \"service\" : \"turn_on\",\t  \"data\" : { \t  \"entity_id\" : $string(topic),\t  \"brightness\" : $number(data.attributes.brightness),\t  \"rgb_color\" : data.attributes.rgb_color\t  }\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":750,"y":300,"wires":[["617c9fba.0465e8"]]},{"id":"8189d023.566ae8","type":"change","z":"9a3534ea.0efa68","name":"","rules":[{"t":"set","p":"lightState","pt":"flow","to":"{ \"service\" : \"turn_off\",\t  \"data\" : { \t  \"entity_id\" : $string(topic)\t  }\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":750,"y":360,"wires":[["d037553b.e27368"]]},{"id":"6b0f35fd.6a8874","type":"change","z":"9a3534ea.0efa68","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"lightState","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":540,"wires":[["57f37873.e15288","388a6732.5b0b48"]]},{"id":"57f37873.e15288","type":"api-call-service","z":"9a3534ea.0efa68","name":"","server":"f65952bb.822ef","version":"1","debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.office_light","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":630,"y":500,"wires":[[]]},{"id":"388a6732.5b0b48","type":"debug","z":"9a3534ea.0efa68","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":590,"y":580,"wires":[]},{"id":"617c9fba.0465e8","type":"api-call-service","z":"9a3534ea.0efa68","name":"","server":"f65952bb.822ef","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.office_light","data":"{\"color_name\":\"red\",\"brightness\":\"255\",\"flash\":\"long\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1030,"y":300,"wires":[["10b64f4b.8b3aa1"]]},{"id":"10b64f4b.8b3aa1","type":"stoptimer","z":"9a3534ea.0efa68","duration":"15","units":"Second","payloadtype":"num","payloadval":"0","name":"","x":150,"y":540,"wires":[["6b0f35fd.6a8874"],[]]},{"id":"d037553b.e27368","type":"api-call-service","z":"9a3534ea.0efa68","name":"","server":"f65952bb.822ef","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.office_light","data":"{\"color_name\":\"red\",\"brightness\":\"255\",\"flash\":\"long\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1030,"y":360,"wires":[["10b64f4b.8b3aa1"]]},{"id":"f65952bb.822ef","type":"server","z":"","name":"HA","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

Not sure if this helps any

Thanks, I’ll have a look. I think one of the issues I had was caffeine related…

Great work! This is almost what I was looking for! Is it possible to add support for shutters? I would like to save the shutter position, close them in the evening automatically and restore the position in the morning. Is that possible? Unfortunately my knowledge of Python is minimal…

It’s just software. Anything is possible! :smiley:

Hmm, I was going to say just copy it and modify it for your specific needs. Unfortunately I don’t use shutters with HA so I have no way of testing any changes. Also this is the first request I’ve had for this, so not a lot of incentive for me to work on it. I’d say, in your situation, you’re probably better off just using the more “traditional” method of using automations and an input_number to save/restore.

Guys, how do you handle motion events? I have a problem:
When pir detects motion, it launches automation: saves current state then change light to bright white, when i go out, after 3min another automation returns previous saved state. This is perfect, problem is, when motion is detected, then not for some time (within 3min), then detected again, it saves last state, so in this scenario, bright white. How to avoid re-save state?

See Script variables, specifically overwrite. The default is True. Set it to False:

service: python_script.light_store
data:
  overwrite: False
  ...
1 Like

works perfect, thanks!

1 Like

I’ve got mostly everything up and running. Only issue i now have is restoring a light group back to its original color. The light effect restores back just fine, as does brightness.

Controllers support hs_color, rgb_color and xy_color.

AUTOMATION

- alias: 'Dallas Stars Goal'
  trigger:
    platform: event
    event_type: nhl_goal
    event_data:
      team_id: "25"
  action:
    - delay: '1'
    - service: python_script.light_store
      data: 
        operation: save
        overwrite: true
    - service: media_player.volume_set
      data:
        entity_id: media_player.living_room
        volume_level: 0
    - service: media_player.play_media
      data:
        entity_id: media_player.living_room
        media_content_id: 'http://192.168.1.12:8123/local/audio/horn.mp3'
        media_content_type: 'audio/mp3'
    - service: light.turn_on
      data:
        entity_id: light.kitchen_counter_lights
        brightness: 255
        rgb_color: [0, 255, 0]
        effect: Flash
    - delay: "50"
    - service: python_script.light_store
      data:
        operation: restore

SCRIPT

save_lights:
  alias: Save light state
  sequence:
    - service: python_script.light_store
      data_template:
        store_name: kitchencounter_lights
        entity_id: 
          - light.kitchen_counter_lights

restore_lights:
  alias: Restore light state
  sequence:
    - service: python_script.light_store
      data_template:
        store_name: kitchencounter_lights
        operation: restore

So which are you using? The automation? Or the scripts? Or both?

For the automation I can see where this might not work correctly, given the documented caveat. If you have any Light Groups you must list entity_id’s.

Also, what attributes does light.kitchen_counter_lights have?

Both in this case, the automation is calling the script to store the current state and then restore at the end of a 50 second delay.

There are in fact 2 entities contained within light.kitchen_counter_lights.

light:
  - platform: group
    name: Kitchen Counter Lights
    entities:
      - light.shelly_shrgbw2_b0dbc7
      - light.shelly_shrgbw2_b08344

Rather, you should specify the list of individual light entity_id’s, or the light group
this is what threw me off i guess. Figured i was ok since i listed the light group in both locations, rather than a mix.

Should the separating the ID’s be both within the Script and the Automation?

Edits made:

SCRIPT

save_lights:
  alias: Save light state
  sequence:
    - service: python_script.light_store
      data_template:
        store_name: kitchencounter_lights
        entity_id: 
          - light.shelly_shrgbw2_b0dbc7
          - light.shelly_shrgbw2_b08344

AUTOMATION

    - service: light.turn_on
      data:
        entity_id:
           - light.shelly_shrgbw2_b0dbc7
           - light.shelly_shrgbw2_b08344
        brightness: 255
        rgb_color: [0, 255, 0]
        effect: Flash

No, I didn’t mean python_script.light_store, I meant the scripts you posted. The automation you posted does not call the scripts you posted. I was wondering if you were using the automation you posted, or the scripts you posted, or both, and which ones were not working for you.

In the automation you posted, when it calls python_script.light_store, in both cases it is not specifying the entity_id parameter. That is a problem if you have one or more Light Group entities in your system. So for that automation you at least should add:

entity_id: light.kitchen_counter_lights

to both calls to python_script.light_store. (It’s also ok to list out the individual light entities, but it should be ok to use the light group.)

In the scripts you posted, you’re specifying entity_id in the first (save), but not in the last (restore.) You should add entity_id to the second script, just like in the first.

I also asked:

Maybe i need to do more fishing around to understand how Automations and Scripts work.

Basically i want the automation to call the script after the trigger happens.

This seems broken.

- alias: 'Dallas Stars Goal'
  trigger:
    platform: event
    event_type: nhl_goal
    event_data:
      team_id: "25"
  action:
    - delay: '1'
    - service: python_script.light_store
    - service: media_player.volume_set
      data:
        entity_id: media_player.living_room
        volume_level: 0
    - service: media_player.play_media
      data:
        entity_id: media_player.living_room
        media_content_id: 'http://192.168.1.12:8123/local/audio/horn.mp3'
        media_content_type: 'audio/mp3'
    - service: light.turn_on
      data:
        entity_id: light.kitchen_counter_lights
        brightness: 255
        rgb_color: [0, 255, 0]
        effect: Flash
    - delay: "50"
    - service: python_script.light_store
save_lights:
  alias: Save light state
  sequence:
    - service: python_script.light_store
      data_template:
        store_name: kitchencounter_lights
        entity_id: 
          - light.kitchen_counter_lights
        operation: save

restore_lights:
  alias: Restore light state
  sequence:
    - service: python_script.light_store
      data_template:
        store_name: kitchencounter_lights
        entity_id:
          - light.kitchen_counter_lights
        operation: restore

light.kitchen_counter_lights contains

           - light.shelly_shrgbw2_b0dbc7
           - light.shelly_shrgbw2_b08344

Yes, that’s a good idea. See Automation and Scripts.

What script? python_script.light_store, script.save_lights or script.restore_lights?

Yes, even more so than when we started.

As currently written your automation only calls python_script.light_store, and it’s not passing the necessary parameters. And there is no relationship between the automation and your two scripts ( script.save_lights and script.restore_lights) either. I.e., nothing is calling your scripts.

Front of the automation needs to call `script.save_lights and end after a 50 second delay should be script.restore_lights.

Changed again

- alias: 'Dallas Stars Goal'
  trigger:
    platform: event
    event_type: nhl_goal
    event_data:
      team_id: "25"
  action:
    - delay: '1'
    - service: script.save_lights
    - service: media_player.volume_set
      data:
        entity_id: media_player.living_room
        volume_level: 0
    - service: media_player.play_media
      data:
        entity_id: media_player.living_room
        media_content_id: 'http://192.168.1.12:8123/local/audio/horn.mp3'
        media_content_type: 'audio/mp3'
    - service: light.turn_on
      data:
        entity_id: light.kitchen_counter_lights
        brightness: 255
        rgb_color: [0, 255, 0]
        effect: Flash
    - delay: "50"
    - service: script.restore_lights

That’s looking better. :slightly_smiling_face:

Still, what attributes does light.kitchen_counter_lights have? I’m not asking which entities it is made up of. I’m asking what attributes it has. Look on the STATES tab of the Developer Tools page.

Messaged you on Discord to say thank you by the way, appreciate the help here.

effect_list:
  - Meteor shower
  - 'Off'
  - Flash
  - Gradual change
brightness: 79
hs_color:
  - 0
  - 0
rgb_color:
  - 255
  - 255
  - 255
xy_color:
  - 0.323
  - 0.329
white_value: 0
effect: 'Off'
friendly_name: Kitchen Counter Lights
supported_features: 149

I’m not on Discord all that often. I prefer this forum.

Thanks for the list of attributes. So, when the current state is saved, it will be saving that composite hs_color (assuming you specify the light group in the save/restore operations.) Does it seem like that reasonably represents the color of the lights? If so, then that should work. If not, then you’ll probably need to list out the lights individually in the entity_id parameter for the save and restore operations instead of using the light group.