Force Lights On/Off

I’m trying to create an automation to keep exterior landscaping/building lights off during the day and lights on at night if a user presses the physical button on the switch. That being said, if someone presses a button on one of the many tablets, I’d like that action to be executed.

I was thinking of utilizing the event trigger and capture the context data as it exposes if a user executed the action where a physical switch shows Null.

My question is if there is an elegant way of scripting this that won’t chew threw processing power? Has anyone scripted something similar.

Thanks

What are you using for the switch? i.e. a Shelly relay or some other smart outlet?

This should help you get started. It turns off the switch if it’s turned on during the day and turns it on if turned off at night. It will do this only if the user controlled the physical switch as opposed to via the UI (the condition determines how the switch was controlled).

alias: Revert switch
id: revert_switch
mode: single
max_exceeded: silent
trigger:
  - platform: state
    entity_id: switch.your_switch
condition:
  - "{{ trigger.to_state.context.id != none }}"
  - "{{ trigger.to_state.context.parent_id == none }}"
  - "{{ trigger.to_state.context.user_id == none }}"
action:
  - service: "switch.turn_{{ 'off' if is_state('sun.sun', 'above_horizon') else 'on' }}"
    target:
      entity_id: switch.your_switch

@123, sweet thank you so much.

Is there a way to target the specific triggered switch in the action in one action vs conditioning the heck out of the automation?

trigger:
  - platform: state
    entity_id: switch.your_switch1
  - platform: state
    entity_id: switch.your_switch2
  - platform: state
    entity_id: switch.your_switch3

FWIW, the equivalent of the multiple State Triggers you posted is this:

trigger:
  - platform: state
    entity_id:
      - switch.your_switch1
      - switch.your_switch2
      - switch.your_switch3

It will trigger if any of the three entities changes state (functions the same way as your version).

If you need to know which one of the three entities triggered, that information is provided by the trigger object. For example, trigger.entity_id reports the entity_id (of the entity that triggered the automation). trigger.to_state.state reports the value it changed to (its previous state value is in trigger.from_state.state). For more information, refer to Automation Trigger Variables and State Objects.

What is your requirement for knowing which entity triggered the automation?

@123 Awesome, thanks.

The thought was I’d need to know which one was triggered in order to turn that specific light off that passed the conditions. If I blanket turn them all on or off, I might be actioning a light that was manually turned on via a user/automation.



alias: Revert switch
id: revert_switch
mode: single
max_exceeded: silent
trigger:
  - platform: state
    entity_id:
      - switch.your_switch1
      - switch.your_switch2
      - switch.your_switch3
condition:
  - "{{ trigger.to_state.context.id != none }}"
  - "{{ trigger.to_state.context.parent_id == none }}"
  - "{{ trigger.to_state.context.user_id == none }}"
action:
  - service: "switch.turn_{{ 'off' if is_state('sun.sun', 'above_horizon') else 'on' }}"
    target:
      entity_id: "{{ trigger.entity_id }}"

If you have a mix of switch and light entities, change the service call from switch.turn_ to homeassistant.turn_ because it supports a broad range of domains.

Thanks @123 so much.

One final question, I’m mixing lights and Switch’s in the trigger list. Is there template to determine if the trigger is a light or switch in one line kinda like the sample with the sun above or below? Would be slick to use that to use switch.turn_off or light.turn_off depending on the trigger

“switch.turn_{{ ‘off’ if is_state(‘sun.sun’, ‘above_horizon’) else ‘on’ }}”

As mentioned at the end of my previous post, use homeassistant for the service call instead of switch or light. It can turn on/off many domains (switch, light, group, fan, etc).

However, if you really want to know the domain of whichever entity triggered the automation, use trigger.to_state.domain. So the template would look like this (but I don’t recommend it; simply use homeassistant)

service: "{{ trigger.to_state.domain }}.turn_{{ 'off' if is_state('sun.sun', 'above_horizon') else 'on' }}"