Python_script to save and restore switches and lights

As far as i can tell, the HS_Color values are fine.

Looks like this borked the light.turn_on and the Media_player actions. No longer getting a change to green, nor flashing lights or the media playing.

Removing - service: script.save_lights brings back the expected function of the automation, minus the script.restore_lights since the save function is removed.

- alias: 'Dallas Stars Goal'
  trigger:
    platform: event
    event_type: nhl_goal
    event_data:
      team_id: "25"
  action:
    - delay: '1'
    - service: script.save_lights
    - service: light.turn_on
      data:
        entity_id: light.kitchen_counter_lights
        brightness: 255
        rgb_color: [0, 255, 0]
        effect: Flash
    - 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'       
    - delay: "50"
    - service: script.restore_lights

.

Can you invoke the scripts separately? E.g., go to the STATES tab of the Developer Tools page, find script.save_lights in the list, click on the “more info” icon to the left of it, and in the window that pops up, click on the EXECUTE button. Does that cause any errors (look in the LOGS tab)? Does it create the kitchencounter_lights.xxx entities (look in the STATES tab)? Do the same for script.restore_lights. Basically make sure these scripts work first before trying to figure out why you’re having problems calling them from an automation. Unless you’ve changed something in those scripts since you last posted them, I don’t see any problems with them. (And if you have changed them then you should re-post them.)

no scripts showing… :upside_down_face:

Well, that’s a problem. :stuck_out_tongue:

Are you remembering to restart HA when you change YAML files? Are you sure the file that contains the script definitions is getting included?

Ok, so onto a new/old issue. Brightness and Effect revert correctly to the state before the scripts. Color, however, does not.

If you want me to continue helping you actually need to answer the questions I ask. Just showing me a snippet of a file that contains a couple of scripts does not answer the questions I asked.

Yes, originally i did not have scripts within the config, but have changed that and restarted

Now, yes. Its within the states engine and has a last run time that corresponds to the when i manually run the automation

Throwing in the towel. No point in stressing you and I out over the last 1% of a function that already runs to my expectations.

Appreciate the time.

Hey there, how about covers? Can I use the script to save cover positions as well?

Not as currently written.

I see. I decided to use home-assistant-variables from HACS directly :slight_smile:

Ok. BTW, the custom variables component I suppose has some nice features, but I’ve been using HA for over a year and have done just fine with HA’s built-in “variables” (i.e., input_boolean, input_number, input_text, etc.)

and counter! :wink:

1 Like

A similar functionality was just added to the core.
In release 0.103.0 a new parameter called snapshot_entities was added to the scene.create-service. This allows you to save the states of entities to a temporary scene and re-apply them by later calling the scene.

1 Like

Do you know if this will persist the last “on” state attributes on entities, if called when the entity is off?

What “this”? The python_script described in this topic, or the new scene.create service with snapshot_entities parameter?

If the former, no. If the entity is off, it’s off and has no attributes to save.

I was asking about the new scene creator with snapshot. - after tests it doesn’t have last “on” attributes, so workaround I use is to do 2 snapshots, first as the system is (mix of on, off) and then turn all lights on, and take the on snapshot, this way I can then do what I need with the lights (in my case flash the alarm lights), and when I’m done I first restore the on snapshot to re-set the on attributes, and then the other one to turn off what was turned off, and keep on what was turned on.

I was about to give the python script a go and saw your post.
I can confirm that the snapshot_entities worked for me. In case this helps others.

I have been using an automation for over a year now to turn on all outside lights for 10 minutes if any of my Ring cameras detected motion. Issue I always had was then I had to turn them all off, even if they were intentionally left on.
Taking snapshot worked perfectly for this, and was very easy to setup just following simple example from the link you gave.
It doesn’t work with light groups though, you need to list each entity separately.

Thanks for alerting us. Saved me a LOT of time. Since I had never set up python scripts before.
Downside is that I still don’t know how to do that. :slight_smile:

Hi, thanks for sharing this. I was using python, but I had problem with colored lights which were off. The script was saving only on/off and not the color, so when i switched again i found colors that i don’t want. Could you please give me an example with the new double “scene snapshot” method? Sorry, newbie here :grinning:

Btw thanks. What a beginner mistake, that I didn’t know about them :woman_facepalming: