Conditional element, to control light states

I have automation turning lights on at a certain time, then off a couple hours later. However, if we are out for dinner, and come home after the lights, have already turned off, I would like them to turn on again with proximity.

So I thought to add a numeric counter, which would reflect the state of the lights:
1 = off
2 = turned on by automation sequence
3 = after dark, and not on, proximity called to turn on.
4 = Turned on by proximity, therefore stay on for 15 mins, then revert to state 1.

I see in helper there is a numeric element that we can select, but I cannot see how to change or set that with automation that gets called, etc…

Am I just missing this or have I not dug deep enough?

Hi,

Do you mean input_number ? If yes, you have a service called input_number.set_value that you can call with your automation.

service: input_number.set_value
data:
  value: 4
target:
  entity_id: input_number.test

The other option is to drop the numbers altogether and use the much more descriptive strings as options in an input select. Automations can change the selected option. e.g.

input select:

rumpus_scene:
  name: Rumpus Scene
  options:
  - 'Off'
  - 'Automatic'
  - 'Bright'
  - 'Night Light'
  - 'Watch Movie'
  - 'Zen'

Examples of automation use:

- id: feb566b0-a229-4d3b-b4af-5d0a984ff567
  alias: 'Rumpus Scene Select'
  trigger:
    platform: state
    entity_id: input_select.rumpus_scene
  action:
  - service: lifx.effect_stop
    entity_id: group.all_rumpus_lights
  - service: scene.turn_on
    data:
      entity_id: >
        {% if states('input_select.rumpus_scene') == 'Off' %}
          scene.rumpus_off
        {% elif states('input_select.rumpus_scene') == 'Automatic' %}
          scene.rumpus_automatic
        {% elif states('input_select.rumpus_scene') == 'Bright' %}
          scene.rumpus_bright
        {% elif states('input_select.rumpus_scene') == 'Night Light' %}
          scene.rumpus_night_light
        {% elif states('input_select.rumpus_scene') == 'Watch Movie' %}
          scene.rumpus_watch_movie
        {% elif states('input_select.rumpus_scene') == 'Zen' %}
          scene.rumpus_zen
        {% else %}
          scene.rumpus_automatic
        {% endif %}
- id: 7d31b418-bef1-4958-be5f-99508092baf8
  alias: 'Rumpus Lights Auto On'
  trigger:
  - platform: state
    entity_id: binary_sensor.pir_rumpus
    from: 'off'
    to: 'on'
  condition:
  - condition: state
    entity_id: light.all_rumpus_room_lights
    state: 'off'
  - condition: not
    conditions:
      condition: state
      entity_id: media_player.cinema_kodi
      state: 'playing'
  - condition: state
    entity_id: input_select.rumpus_scene
    state: 'Automatic'
  - condition: state
    entity_id: binary_sensor.dark_outside
    state: 'on'
  action:
  - service: homeassistant.turn_on
    data:
      entity_id: >
        {% if  is_state('binary_sensor.night_vision_active', 'on') %}
          scene.rumpus_night_light
        {% else %}
          scene.rumpus_automatic
        {% endif %}
- id: a9c43e5f-3e67-4e4b-acdd-7b8161e82ee5
  alias: "Rumpus Movie Scene On"
  trigger:
    platform: state
    entity_id: media_player.cinema_kodi
    to: 'playing'
  condition:
    condition: state
    entity_id: light.all_rumpus_room_lights
    state: 'on'
  action:
  - service: input_select.select_option
    data:
      entity_id: input_select.rumpus_scene
      option: "Watch Movie"
- id: a7fb2daf-e460-4fd6-9bae-4d774800c352
  alias: "Rumpus Movie Scene Off"
  trigger:
    platform: state
    entity_id: media_player.cinema_kodi
    from: 'playing'
    for: 1
  action:
  - service: input_select.select_option
    data:
      entity_id: input_select.rumpus_scene
      option: "Automatic"

scene:

- name: Rumpus Automatic
  entities:
    light.bar_downlights:
      state: 'on'
      brightness: 255
      color_temp: 370
    light.cinema_downlights:
      state: 'on'
      brightness: 255
      color_temp: 370
    light.lifx_cinema_left:
      state: 'off'
    light.lifx_cinema_right:
      state: 'off'
    light.lifx_bar_lamp:
      state: 'on'
      brightness: 255
      color_temp: 667
- name: Rumpus Zen
  entities:
    light.bar_downlights:
      state: 'on'
      brightness: 255
      rgb_color: [255,165,0]
    light.lifx_bar_lamp:
      state: 'on'
      brightness: 255
      rgb_color: [255,165,0]
    light.cinema_downlights:
      state: 'on'
      brightness: 255
      rgb_color: [255,165,0]
    light.lifx_cinema_left:
      state: 'off'
    light.lifx_cinema_right:
      state: 'off'

thank you, very obvious now that you pointed it out…

1 Like

I like that, a different approach to what I was thinking. I will give this a go. Thanks…

For automations like this I like to use the Chooser. Thomas Lovén did a great presentation at the Home Assistant conference a few years ago. Search youtube for “Make smarter automations, not more automations - Home Assistant Conference 2020” and you should find it. Using this methodology my automations are now much more reliable and simpler. There is one automation for each “thing” that can handle all eventualities.

Basically for the scenario above I would map out how I want the lights to behave for any given set of input conditions. The automation should be able to be run whenever an input condition changes and give the correct result for the lights. Watch the video as Thomas explains it far better than I ever could.

I see the following affecting these lights, Sun is up, After Sunset, Before 10pm, Proximity.

One key is setting the automation to mode: restart if something changes. This will then handle all the little edge cases like the sun coming up right after you get home etc.

Sun is Up After Sunset Before 10pm Proximity Light State
Yes Off
Yes Yes On
No Off
No to Yes On, wait 15 mins, Off

I have a “Sun up” binary_sensor which makes this thing a whole lot easier.

sensors:
  sun_up:
    friendly_name: "Sun Up"
    value_template: >-
      {{ state_attr('sun.sun', 'elevation')|float > 0 }}

I like to use the visual editor for my automations.

image

Define each trigger. Again, the automation should be able to give the correct output for the lights each time it is run.

  1. Sun Up

  2. Sunset
    image

  3. Time 10pm (plus a second)
    image

  4. Proximity Not home to Home (whatever your proximity is).
    Add a trigger id to this one since we are looking for not home to at home.

The point of the triggers is to get the automation to run through and evaluate each time something changes.

Then the key in the actions is the chooser. Again, watch the video on youtube.

I refer back to the table and there will be one Option per row in the table. Remember that the order is important. Once a set of criteria matches it won’t go any further. Define easy things first like Sun comes up > light goes off.

Option 1 Conditions

Option 1 Actions

Option 2 Conditions

Option 2 Actions

Option 3 Conditions (proximity nobody home)
image

Option 3 Actions

Option 4 Conditions
Here we reference the trigger id to make sure we only hit this block when the proximity has changed from nobody home to someone home
image

Option 4 Actions

image

image

I hope this helps someone else. This methodolgy has much improved the stability of my automations since I have only one automation for each thing. Much more readable as well. The hardest part is coming up with the table in the first place of how the lights should be based on the criteria.

1 Like

@VPC thank you very much for the detailed explanation. I agree, I do like the chooser, I have been using them a little for automations as people enter and exit zones. It is much cleaner to use this approach for that. I had not thought to useit for my light problem as I am choosing between multiple things. But your explanatin is great, and I will watch the vide and try to implement the right tablefor my needs.

Thanks again…