Help with light template state from triggering a switch

I have been working for hours on attempting to get the status of a light from triggering a switch. I have built a remote that uses a switch to turn on a light. Problem is that when I trigger the switch it just toggles the command and then goes back to off. So I am attempting to use the light template to create a status of the light and show it in home assistant. There is where my struggles lie. What is wrong with my code here?

light:
  - platform: template
    lights:
      aqled_light:
        friendly_name: "Aquarium LED"
        value_template: "{{ is_state_attr('switch.aqled_power', 'on') }}"
        turn_on:
          service: light.turn_on
          target:
            entity_id: light.aqled_light
        turn_off:
          service: light.turn_off
          target:
            entity_id: light.aqled_light

Are you sure that 'on' is an attribute? Try:

"{{ is_state('switch.aqled_power', 'on') }}"

You’re going to need something to store the state of the switch. Otherwise it’ll always switch back to off if the state of the switch switches back to off

Which method is best to store the state of the switch petro? Binary sensor? I was trying to do that with template, but haven’t been successful obviously.

Yes pedolsky, I can hit the button to fire off turning on the lights and it registers for a second or so to a state on from off.

Any help would be appreciated!

Any suggestion on how I should proceed?

You have defined a Template Light called: light.aqled_light

This Template Light, when turned on (or off), commands itself to turn on (or off).

        turn_on:
          service: light.turn_on
          target:
            entity_id: light.aqled_light
        turn_off:
          service: light.turn_off
          target:
            entity_id: light.aqled_light

That seems rather pointless.

That’s a textbook example of a Template Light (or Template Switch) with an improperly configured value_template.

When you turn on a Template Light, its turn_on service controls something that is expected to report back on to the value_template. If it fails to do that (and it most certainly will fail in your Template Light) the classic behavior is that the Template Light will briefly change state to on and then immediately revert to off.

The problem here is that you have configured this Template Light with turn_on and turn_off services that effectively do nothing. They control nothing that can report back its changed status to the Template Light’s value_template.


EDIT

This might work for you but, to be honest, I’m not sure why you would need a Template Light to mimic an existing switch.

light:
  - platform: template
    lights:
      aqled_light:
        friendly_name: "Aquarium LED"
        value_template: "{{ is_state('switch.aqled_power', 'on') }}"
        turn_on:
          service: switch.turn_on
          target:
            entity_id: switch.aqled_power
        turn_off:
          service: switch.turn_off
          target:
            entity_id: switch.aqled_power

I don’t think your edit will work if what he says about the switch is true.

This insinuates that it’s a momentary switch

You can use an input boolean to capture the state and use that in the value_template. Turn on the input boolean and the switch in the turn on section. In the turn off section turn off the boolean and turn on the switch.

The other option is to check the hardware that makes the switch. Maybe there’s a setting that changes it from a momentary switch to an assumed state switch

I would ask different question

Why you need the state of so called “switch”? isn’t it the light which - in your case - represents the state you are after?

I agree with you. The need for the Template Light is unclear, its configuration is incorrect, and the behavior of switch.aqled_power sounds suspect.


Gene Kranz: Let’s look at this thing from a… um, from a standpoint of status. What do we got on the spacecraft that’s good?

[pause]

Sy Liebergot: I’ll get back to you, Gene.

Ok to make sure we are clear here on what I am doing. I have a LED light that is not smart, but is controlled via an ir remote. So to control it, I have captured all the commands in ESPHome via the remote and inputted it into an esp32 with an IR transmitter sitting by the light. To trigger the “dumb” light, I use the switch.aqled_power. It sends a command to the ir transmitter and the light is switched on. Problem is that once the switch is fired, the status of switch.aqled_power switches off. Petro is correct, what I would like is a way to capture whether the light is on or off in home assistant to keep track of it’s status. That way my automations can run correctly and it is more useful.

Petro, I think your first option is best but if you could point on an example I would appreciate it. Yes I had the light on a sonoff, but I can change the color of this LED strip that is built for aquariums. I could replace that as well, but am trying to stay within the hardware that I have. The IR transmitter does a lot more and has a lot of aquarium sensors in it. So I was thinking outside the box on wanting to control this light.

I just can’t keep track of it in home assistant is me problem…

[/quote]

Here is the switch in EspHome

  - platform: template
    name: "aqled_power"
    id: aqled_power
    turn_on_action:
      - remote_transmitter.transmit_raw:
          code: [ 8974, -4445, 587, -537, 589, -538, 644, -1584, 588, -538, 646, -459, 563, -560, 589, -536, 644, -463, 559, -1690, 644, -1584, 589, -537, 645, -1584, 588, -1643, 585, -1663, 645, -1584, 589, -1643, 585, -538, 588, -539, 562, -542, 560, -563, 
                  589, -538, 644, -461, 560, -1690, 642, -463, 559, -1689, 644, -1585, 589, -1642, 586, -1664, 562, -1666, 589, -1642, 585, -538, 589, -1643, 585]
          carrier_frequency: 38kHz

Create an input_boolean called input_boolean.aqled and use this Template Light configuration:

light:
  - platform: template
    lights:
      aqled_light:
        friendly_name: "Aquarium LED"
        value_template: "{{ is_state('input_boolean.aqled', 'on') }}"
        turn_on:
          - service: homeassistant.turn_on
            target:
              entity_id:
                - switch.aqled_power
                - input_boolean.aqled
        turn_off:
          - service: homeassistant.turn_off
            target:
              entity_id:
                - switch.aqled_power
                - input_boolean.aqled

But…what if someone switches the light off with the remote after the light is switched on with the switch?

Now the HA status and the actual light status are out of synch.

That’s why the use of work arounds for things like this are iffy at best.

1 Like

just add assumed_state: true to your switch in esphome and you’ll have a state based switch. If the same signal is sent to ‘turn off’ then you need to duplicate the turn on action in the turn off action area.

  - platform: template
    name: "aqled_power"
    id: aqled_power
    assumed_state: true
    turn_on_action:
      - remote_transmitter.transmit_raw:
          code: [ 8974, -4445, 587, -537, 589, -538, 644, -1584, 588, -538, 646, -459, 563, -560, 589, -536, 644, -463, 559, -1690, 644, -1584, 589, -537, 645, -1584, 588, -1643, 585, -1663, 645, -1584, 589, -1643, 585, -538, 588, -539, 562, -542, 560, -563, 
                  589, -538, 644, -461, 560, -1690, 642, -463, 559, -1689, 644, -1585, 589, -1642, 586, -1664, 562, -1666, 589, -1642, 585, -538, 589, -1643, 585]
          carrier_frequency: 38kHz
    turn_off_action:
      - remote_transmitter.transmit_raw:
          code: [ 8974, -4445, 587, -537, 589, -538, 644, -1584, 588, -538, 646, -459, 563, -560, 589, -536, 644, -463, 559, -1690, 644, -1584, 589, -537, 645, -1584, 588, -1643, 585, -1663, 645, -1584, 589, -1643, 585, -538, 588, -539, 562, -542, 560, -563, 
                  589, -538, 644, -461, 560, -1690, 642, -463, 559, -1689, 644, -1585, 589, -1642, 586, -1664, 562, -1666, 589, -1642, 585, -538, 589, -1643, 585]
          carrier_frequency: 38kHz

Advanced configuration with shortened code...
  - platform: template
    name: "aqled_power"
    id: aqled_power
    assumed_state: true
    turn_on_action: &my_action
      - remote_transmitter.transmit_raw:
          code: [ 8974, -4445, 587, -537, 589, -538, 644, -1584, 588, -538, 646, -459, 563, -560, 589, -536, 644, -463, 559, -1690, 644, -1584, 589, -537, 645, -1584, 588, -1643, 585, -1663, 645, -1584, 589, -1643, 585, -538, 588, -539, 562, -542, 560, -563, 
                  589, -538, 644, -461, 560, -1690, 642, -463, 559, -1689, 644, -1585, 589, -1642, 586, -1664, 562, -1666, 589, -1642, 585, -538, 589, -1643, 585]
          carrier_frequency: 38kHz
    turn_off_action: *my_action

Then, do nothing in home assistant because you now have a switch that works properly. But if you REALLY want a light… configure a light via esphome or use the template switch in this post in conjunction with the esphome configuration above:

I will try the first method to see how it works out. I mean after all, I still could add the sonoff to the light to completely know it’s status. Just thought I would try something different and outside the box to learn more about home assistant and save a sonoff… With each project I work on, home assistant gets easier to learn. I knew I was poking around in the right areas, just couldn’t get it all to work out.