Update the status of a switch from the variation of another entity

Hi,

I have an alarm system that I can control from a switch on/off but the Home Assistant doesn’t know if someone in my family turns on or off the alarm via radio controller.

So i installed a led sensor with ESPhome to know the light variation of the alarm system monitoring the built-in light.

Now I just want to update the switch status directly from the light variation entity.

Can you help me?

In the Template Switch page (Template Switch - Home Assistant) it is described the exact problem that I want to solve but I’m not able to set the template.

I just want to overwrite the switch status using the light entity variation

So is the LED sensor exposed in home assistant? You can use that sensor and create an automation. when the state of the LED changes, you know the alarm is armed. Then you can run the switch.turn_on action to change the switch in home assistant.

Yes, the led is exposed to HomeAssistant.

I can’t use what you suggest because I don’t need to turn on the alarm. The alarm is already on (maybe activated by radio controller) but in Home Assistant appears as off.

Can you post the template you have now or have you not created one? It’s just a matter of setting the state to be the state of your LED using something like this in your template:

value_template: "{{ states('light.my_led') }}"

I don’t have the template…I need help to set it.

I tried with a script that update the status of an helper via boolean request but I’m not able to change the status of the alarm entity.

alias: Aggiornamento Status Allarme
sequence:
  - parallel:
      - if:
          - condition: numeric_state
            entity_id: sensor.nodemcu_allarme_led_allarme
            above: 150
        then:
          - service: input_boolean.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.antifurto_status
      - if:
          - condition: numeric_state
            entity_id: sensor.nodemcu_allarme_led_allarme
            below: 100
        then:
          - service: input_boolean.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.antifurto_status
mode: single

So first look at how and where to create a binary sensor template, then the sensor will be like this:

binary_sensor:
  - name: "Alarm Active"
    unique_id: ef33434a-e2d9-449f-84e9-3b5fed3cf814
    state: "{{ int(states('sensor.nodemcu_allarme_led_allarme'), 0) < 100 or int(states('sensor.nodemcu_allarme_led_allarme'), 0) > 150 }}"

If you want to use your script instead:

alias: Aggiornamento Status Allarme
sequence:
  - if:
      - condition: numeric_state
        entity_id: sensor.nodemcu_allarme_led_allarme
        above: 150
    then:
      - service: input_boolean.turn_on
        metadata: {}
        data: {}
        target:
          entity_id: input_boolean.antifurto_status
  - if:
      - condition: numeric_state
        entity_id: sensor.nodemcu_allarme_led_allarme
        below: 100
    then:
      - service: input_boolean.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: input_boolean.antifurto_status
mode: single

But the script is overkill, you could have done a simple automation, which you still can using the LED value above/below or create the binary sensor as above and use that as your condition.

So let me explain better because I’m a new in HA and I want to learn :slight_smile:

So I have my alarm switch in HA (called switch.antifurto). I can use it to turn on and off the alarm via HA but it’s status sometimes is not correct because I can turn on/off the alarm via radio controller and the integration is not able to update the status.

Using a light sensor (called sensor.nodemcu_allarme_led_allarme) I can detect when the alarm light is on/off.

What I want to have at the end is to update the status of switch.antifurto using the second sensor.

Can you tell me step by step how to do that?

Using the script that I shared with you I can’t update the status; it update the status of a helper that I created following a YouTube video.

alias: example
trigger:
  - id: 'on'
    platform: numeric_state
    entity_id: sensor.nodemcu_allarme_led_allarme
    above: 150
  - id: 'off'
    platform: numeric_state
    entity_id: sensor.nodemcu_allarme_led_allarme
    below: 100
condition: []
action:
  - service: 'switch.turn_{{ trigger.id }}'
    target:
      entity_id: switch.antifurto

I should add this code to my configuration.yaml?

It’s an automation.

  1. I suggest you use the Automation Editor to create a new automation.
  2. Switch the Automation Editor from Visual mode to YAML mode.
  3. Copy the example I posted above and paste it into the Automation Editor’s editing window. Overwrite anything that already exists in the window.
  4. Switch the Automation Editor from YAML mode to Visual mode.
  5. Click the Save button to save the new automation (give it whatever name you want).

I have a similar problem with a garage door.