Automation with variable condition?

Hi there,

I’ve programmed in perl and c but am not yet familiar with the HA language and was hoping for some direction.

I have an aeon multi sensor which I’d like to turn lights on and off in a main living area.

The Lights on automation is no issue - if you walk in the room AND its after dusk AND the light is off, it turns the lights on.

The Lights off automation is more of a problem, in part, but not least because of the bugginess of the aeon multisensor and the fact it may not show movement if someone is sitting relatively still.

So I don’t want the lights turned off (ever) IF they’ve been turned on manually at the switch.

The best way I can think to do this is set a variable if the aeon sensor automation was the device that turned the lights on (rather than the switch / home assistant manually).

i.e. variable var

  • = 0 by default;
  • = 1 if automation ‘lights on’ is triggered.
  • = 0 when automation ‘lights off’ is triggered.

AND automation ‘lights off’ only triggers IF
a) No sensor movement occurs for 3 minutes AND
b) The main light is on and
b) var == 1.

Below is my current automation. I am lost on the setting, retention and use of variables as conditions / triggers and would really appreciate some guidance!

Thanks

- id: '1567421841803'
  alias: Light On
  trigger:
  - entity_id: sensor.aeon_movement
    platform: state
    to: '8'
  condition:
  - after: sunset
    condition: sun
  - condition: state
    entity_id: light.fibaro_system_fgd212_dimmer_2_level_10
    state: 'off'
  action:
  - data:
      brightness: 53
      entity_id: light.fibaro_system_fgd212_dimmer_2_level_10
    service: light.turn_on
- id: '1568377972563'
  alias: Light Off
  trigger:
  - entity_id: sensor.aeon_movement
    for: 00:03:00
    platform: state
    to: '0'
  condition:
  - condition: state
    entity_id: light.fibaro_system_fgd212_dimmer_2_level_10
    state: 'brightness : 53'
  action:
  - data:
      entity_id: light.fibaro_system_fgd212_dimmer_2_level_10
    service: light.turn_off

Home Assistant doesn’t have variables built-in (there are custom components for that), but you can use input_number to accomplish what you’re looking for. Take a look here for how to set up an input_number. Once you’ve set it up, add this to the action block of your Light On automation:

- service: input_number.set_value
  data:
    entity_id: input_number.entity_id
    value: 1

In your Light Off automation, add this to your condition block:

- condition: state
  entity_id: input_number.entity_id
  state: 1

… and add this to the action block:

- service: input_number.set_value
  data:
    entity_id: input_number.entity_id
    value: 0

You also probably don’t need what you already have in the Light Off condition block, but I’ll leave that up to you to decide. Also, if you want to use one of the variable custom components, the steps are similar, you just need to change the service names and set up a variable instead of input_number.

Thanks Tediore!!
I have to wait until evening to try it but that looks like a winner.
Yes, there is a bit of legacy conditions in the light off block - I was getting frustrated by variables and was thinking to use a specific brightness value as a condition instead, I should have cleared that out before I posted here.
I’ll confirm this works tonight!
Brendan.

This particular application only requires storing two states. Any particular reason you chose to suggest using an input_number instead of an input_boolean?

Ah yeah, input_boolean would be better. Whoops. I was thinking literally based on what OP was asking for.

No sweat; it’ll work either way. The advantage of an input_boolean is it requires a tiny bit less info for its configuration. Anyway, it’s all good.

Thanks both. The recommendations didn’t trigger the lights, so I removed the after sunset setting and tried that during the day.
I am now getting the following error in log

Error while executing automation automation.light_off. Service not found for call_service at pos 2: Unable to find service input_number/set_value

2:27 PM helpers/script.py (ERROR)

Error while executing automation automation.light_on. Service not found for call_service at pos 2: Unable to find service input_number/set_value

2:27 PM helpers/script.py (ERROR)

This is based on the new automation (which doesn’t throw syntax errors of itself).

- id: '1567421841803'
  alias: Light On
  trigger:
  - entity_id: sensor.aeon_movement
    platform: state
    to: '8'
  condition:
  - condition: state
    entity_id: light.fibaro_system_fgd212_dimmer_2_level_10
    state: 'off'
  action:
  - data:
      brightness: 53
      entity_id: light.fibaro_system_fgd212_dimmer_2_level_10
    service: light.turn_on

  - service: input_number.set_value
    data:
     entity_id: input_number.entity_id
     value: '1'

- id: '1568377972563'
  alias: Light Off
  trigger:
  - entity_id: sensor.aeon_movement
    for: 00:03:00
    platform: state
    to: '0'

  condition:
  - condition: state
    entity_id: input_number.entity_id
    state: '1'

  action:
  - data:
      entity_id: light.fibaro_system_fgd212_dimmer_2_level_10
    service: light.turn_off
  - service: input_number.set_value
    data:
      entity_id: input_number.entity_id
      value: 0

Thank you again - fyi I am away from home for the next couple of days which might be why I am tardy replying.
Brendan.

You got this error because you didn’t create an input_number.

You need to create an input_number first and then specify the entity_id of this input_number in your code.

Or you can create an input_boolean and do the same thing as @123 suggested.

Thank you @Burningstone // @123 // @Tediore
In the end I opted for the input_boolean and used the input_boolean.turn_off / input_boolean.turn_on services as it seemed neater.
I really appreciate all your help.