Automation with device state recording

Hello,

I’m new to HA. I’ve been able to add my sonoff switches and try some simple automations.

What i’d like to create now is an automation that makes the next steps:

  • Save the state of a set of (let’s say three) sonoff switches in variables.
  • Switch off all of them.
  • Wait for a defined period of time.
  • Restore initial state of the sonoff switches.

I’d like to use a remote RF linked to a sonoff RF bridge as trigger, but from what I’ve read I have to hack with Tasmota (please, correct me if this is not necessary). So, I’d use another trigger system, as by now, I don’t want to change anything in hardware/firmware.

Thank you in advance. Any help will be welcome.
Miguel

Don’t save the states in variables. Save the switch states in a scene on the fly that can be restored later:

See the second example here: https://www.home-assistant.io/integrations/scene/#creating-scenes-on-the-fly

1 Like

You can do something like this.

alias: 'Sonoff Sequence '
description: ''
mode: single
trigger:
  - platform: state
    entity_id: binary_sensor.trigger
condition: []
action:
  - service: scene.create
    data:
      scene_id: before
      snapshot_entities: 'switch.sonoff1,switch.sonoff2,switch.sonoff3'
  - service: switch.turn_off
    data: {}
    entity_id: 'switch.sonoff1,switch.sonoff2,switch.sonoff3'
  - delay: '00:01:00'
  - service: scene.turn_on
    data: {}
    entity_id: scene.before
2 Likes

Your exapmle has a missing ':

  - delay: '00:01:00 
  - service: scene.turn_on
    data: {}
    entity_id: scene.before
2 Likes

Thank you!

I still hadn’t seen the use of scenes.

I’ll try.

Yeah scenes were just an easy way to set device states without having to know the services required until that particular ability was introduced. It’s one of the best enhancements and makes scenes a really powerful tool.

1 Like

If you are not seeing any service like scene.create , you would need to enable it by adding scene: !include scenes.yaml to configuration and then create scenes.yaml in config folder.

2 Likes

I’ve tried the next code:

- id: '1611335123017'
  alias: heaters off
  description: ''
  mode: single
  trigger:
  - platform: state
    entity_id: switch.sonoff_1000caee2e_4
    from: 'off'
    to: 'on'
  condition: []
  action:
   - service: scene.create
    data:
      scene_id: before
      snapshot_entities: 'switch.sonoff_1000caee2e_1,switch.sonoff_1000caee2e_2,switch.sonoff_1000caee2e_3'
  - service: switch.turn_off
    data: {}
    entity_id: 'switch.sonoff_1000caee2e_1,switch.sonoff_1000caee2e_2,switch.sonoff_1000caee2e_3'
  - delay: '00:01:00'
  - service: scene.turn_on
    data: {}
    entity_id: scene.before

But I get the next error:

2021-01-22 18:26:38 ERROR (SyncWorker_0) [homeassistant.util.yaml.loader] while parsing a block collection
in "/config/automations.yaml", line 12, column 4
expected <block end>, but found '<block mapping start>'
in "/config/automations.yaml", line 13, column 5
2021-01-22 18:26:38 ERROR (MainThread) [homeassistant.bootstrap] Failed to parse configuration.yaml: while parsing a block collection
in "/config/automations.yaml", line 12, column 4
expected <block end>, but found '<block mapping start>'
in "/config/automations.yaml", line 13, column 5. Activating safe mode

:roll_eyes:

data should be inline with service. It’s off by one space.

   - service: scene.create
     data:
1 Like

Oh, thanks! It’s working now :slight_smile:

Going on with the automation in this thread, I have a little issue that maybe has no easy solution. In any case, I ask just in case…

Imagine that while the delay is waiting after the state is saved in the ‘before’ scene, I have another automation that based on a scheduler (really I use schedule cards), the state of one of the switches changes to another desired state. When the delay finally finishes, the previous state of the switch is set. As a result the final state is not the desired one.

To put you in situation, I think it’s better to describe the scenario I have:

I have several electrical heaters in my flat. I schedule their use to take profit of the cheaper electricity periods of the day while avoiding to surpass my hired power limit.
Sometimes I have to use some appliances, like the microwave, and of course I want to use it at any time. So, the idea behind this thread was to create an automation that saves the state of the heaters, switch them off for a period of time (maybe 3 minutes in the case of the microwave), and after that period, recover the state of the heaters.
As I said the hole in this approach is what I described above.

Any ideas will be welcome :blush:

We can skip the scheduled operation of the heater while this automation is in progress. This can be done by creating an input_boolean and adding the action to switch it on at the start of this automation and at the last turning it off. In the scheduler card you can put a condition when to start the heater. I dont know how useful this can be.

But if you have your scheduling operations done with automations in HA, we have the option to delay the scheduled operations till this particular operation has been completed. We just needed to use the choose action type.

1 Like

Great! Thank you! This sounds very good.

I make the schedules with ‘scheduler cards’. I don’t know if I can make the delay and the action you say. In many l any case, I’ll try tomorrow.

Even with schedule card we can delay the scheduled operations but it would be lengthy if you have many schedules. In a day how many times you have scheduled operations?

1 Like

I have 4 devices with about 20 scheduled operations (counting on and off altogether).

20 number of automation would just complicate things. so its better you implement it as we discussed earlier. If anything keep us posted… :+1:

I’ve been trying what you said with this test automation:

- id: '1611593229900'
  alias: test switch off heater 1
  description: ''
  trigger:
  - platform: time
    at: '18:15'
  - platform: time
    at: '19:30'
  - platform: time
    at: '20:40'
  condition: []
  action:
  - condition: state
    entity_id: input_boolean.microondas_boolean
    state: 'off'
  - service: switch.turn_off
    data: {}
    entity_id: switch.sonoff_1000caee2e_4
  mode: single

With this approach it doesn’t make the action if the input_boolean is on, but it doesn’t delay the action until the input_boolean is set to off.

Could you please give me some directions to get what you suggested?

I think wait_template can be used for what I want, but I cannot find the syntax of the template to use to wait until input_boolean has off state.

Try something like this. Here I have used the choose action type and wait until together.

alias: test switch off heater 1
description: ''
trigger:
  - platform: time
    at: '18:15'
  - platform: time
    at: '19:30'
  - platform: time
    at: '20:40'
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.microondas_boolean
            state: 'off'
        sequence:
          - service: switch.turn_off
            data: {}
            entity_id: switch.sonoff_1000caee2e_4
      - conditions:
          - condition: state
            entity_id: input_boolean.microondas_boolean
            state: 'on'
        sequence:
          - wait_for_trigger:
              - platform: state
                entity_id: input_boolean.microondas_boolean
                from: 'on'
                to: 'off'
            continue_on_timeout: false
          - service: switch.turn_on
            data: {}
            entity_id: switch.sonoff_1000caee2e_4
    default: []
mode: single
1 Like

In the meantime, I’ve tried this code and it seems to work:

- id: '1611593229900'
  alias: test switch off heater 1
  description: ''
  trigger:
  - platform: time
    at: '1:14'
  - platform: time
    at: '1:15'
  - platform: time
    at: '1:16'
  condition: []
  action:
  - wait_template: '{{ is_state(''input_boolean.microondas_boolean'',''off'') }}'
    timeout: 00:05:00
    continue_on_timeout: true
  - service: switch.turn_off
    data: {}
    entity_id: switch.sonoff_1000caee2e_4
  mode: single

I don’t know if I’m missing anything.

Could I use any of the approaches with scheduler-cards? The thinf is that sometimes I have to change the times of the schedule, or activate/deactivate some intervals, so it’s much more convenient schedurler-cards than plain automations. :woozy_face: