Automation: How create new state or 'global variable'?

Hi there!
Please advice me how to solve my task.

TASK:
I have Sonoff Relay.
I have motion sensor.
I have remote button.

There are two modes:

  1. Motion sensor goes on and Sonoff Relay turns on too. Motion sensor goes off and Sonoff Relay turns off too.
    That is ok. No problem.
  2. Sometimes I want to use remote button to forbid Sonoff Relay turns off by motion sensor.
    I press remote button → Sonoff Relay will turn on and will be turned on till next press of remote button instead of motion sensor states.

How to do so?
My idea was to create some new state, as example SUPERlight=“on/off”. Pressing remote button will turn on Sonoff Relay and will change SUPERlight state to “on”.
In automation for motion sensors will be condition: work only when SUPERlight state=“off”.

How to create new state?
The same I could do with global variable. How to create global variable?

Or maybe you give me some other idea of programming specific of home assistant. Thanks for reading this!

I’m not sure that I get you right but wouldn’t it be a solution if you keep the first automation as it is and build does the following:

  1. Button is pushed once and it sets Sonoff to on AND deactivates the first automation.
  2. A second button push just reactivates the automation.
1 Like

Yes, you are right. How to deactivate automation with motion sensor if button was pressed and switched Sonoff to “on”?

Will post some code there:

- alias: "Turn on Sonoff with motion sensor"
  trigger:
  - platform: state
    entity_id: binary_sensor.datch_dvizh_kukhnia1_occupancy
    from: "off"
    to: "on"
    action:
  - data:
      entity_id: switch.svet_kukhnia_2
    service: switch.turn_on
        
- alias: "Turn off Sonoff with motion sensor"
  trigger:
    platform: state
    entity_id: binary_sensor.datch_dvizh_kukhnia1_occupancy
    from: "on"
    to: "off"
    for:
      minutes: 6
  action:
  - data:
      entity_id: switch.svet_kukhnia_2
    service: switch.turn_off


- alias: "Turn on and off Sonoff with button"
  trigger:
  - payload: 'left'
    platform: mqtt
    topic: zigbee2mqtt/Выключатель_кухня/action
  action:
  - data:
      entity_id: switch.svet_kukhnia_2
    service: switch.toggle

Create an input_boolean called input_boolean.kukhnia to serve as an indicator when the switch was turned on by the button. When input_boolean.kukhnia is on, the automation will not allow the motion sensor to control the switch.

Here is a single automation that controls the switch using the motion sensor and button as triggers and controls the switch based on the input_boolean’s state.

- alias: 'Control Sonoff with motion sensor and button'
  id: 'control_sonoff_motion_button'
  mode: queued
  trigger:
  - platform: state
    entity_id: binary_sensor.datch_dvizh_kukhnia1_occupancy
    from: "off"
    to: "on"
  - platform: state
    entity_id: binary_sensor.datch_dvizh_kukhnia1_occupancy
    from: "on"
    to: "off"
    for:
      minutes: 6
  - platform: mqtt
    topic: zigbee2mqtt/Выключатель_кухня/action
    payload: 'left'
  action:
  - choose:
    - conditions:
      - "{{ trigger.platform == 'state' }}"
      - "{{ is_state('input_boolean.kukhnia', 'off') }}"
      sequence:
      - service: 'switch.turn_{{ trigger.to_state.state }}'
        target:
          entity_id: switch.svet_kukhnia_2
    default:
    - service: homeassistant.toggle
      target:
        entity_id:
        - input_boolean.kukhnia
        - switch.svet_kukhnia_2
2 Likes

thank you very much! It helps! I created indicator!

Solution was in creating input_boolean helper in Configuration-Helpers as you wrote. (Input Boolean - Home Assistant)

1 Like