Turn light On if Off

I have an ESP8266 with an IR Receiver that I use to control various items around the office.

I’ve bought a “Wiz” light to play with and have been able to toggle it on/off and increase/decrease the brightness using an IR Remote. The following code is the bit that increases the brightness on a button press.

  - platform: remote_receiver
    name: "Wiz Light Brighter"
#   Remote Button '+'
    internal: true
    on_release:
      homeassistant.service:
        service: light.turn_on
        data:
          entity_id: light.wiz_test_light
        data_template:
            brightness: '{{states.light.wiz_test_light.attributes.brightness + 10}}'
    filters:
      - delayed_off: 100ms
    pioneer:
      rc_code_1: 0x0009

This works as long as the light is already on. What I’ve not been able to figure out is the Condition statement needed to turn the light on if needed when the “Brightness Increase” button is pressed. I’ve tried several different variations of Conditions but just can’t seem to figure out the correct place to put it or the correct syntax. I’m likely over-thinking/over-complicating this so any help would be appreciated.

Thanks
Ed

Usual question, where is your log?

Not sure calling HA services from esphome is a good idea, generally speaking. At some point, you might forget you do that and wonder what’s happening.

I would send an event instead and, assuming you know in HA if the light is on or off, create an ad-hoc automation.

Here’s the log showing the following sequence of button presses. Each button press returns two log entries. (State ON/State OFF)

  1. Power Toggle (turn light on)
  2. Increase Brightness
  3. Decrease Brightness
  4. Power Toggle (turn light off)
  5. Increase Brightness (with light off)
  6. Decrease Brightness (with light off0

No errors indicated here. The first four steps work as expected. I believe the issue with trying to change the brightness level when the light is off is I’m getting the current brightness level, adding 10 and setting that as the new level. With the light off we can’t get or set the brightness so the function fails.

[07:19:24][D][binary_sensor:036]: 'Wiz Light Toggle': Sending state ON
[07:19:25][D][binary_sensor:036]: 'Wiz Light Toggle': Sending state OFF
[07:19:30][D][binary_sensor:036]: 'Wiz Light Brighter': Sending state ON
[07:19:30][D][binary_sensor:036]: 'Wiz Light Brighter': Sending state OFF
[07:19:34][D][binary_sensor:036]: 'Wiz Light Dimmer': Sending state ON
[07:19:34][D][binary_sensor:036]: 'Wiz Light Dimmer': Sending state OFF
[07:19:38][D][binary_sensor:036]: 'Wiz Light Toggle': Sending state ON
[07:19:39][D][binary_sensor:036]: 'Wiz Light Toggle': Sending state OFF
[07:19:42][D][binary_sensor:036]: 'Wiz Light Brighter': Sending state ON
[07:19:42][D][binary_sensor:036]: 'Wiz Light Brighter': Sending state OFF
[07:19:46][D][binary_sensor:036]: 'Wiz Light Dimmer': Sending state ON
[07:19:46][D][binary_sensor:036]: 'Wiz Light Dimmer': Sending state OFF

I guess I should have explained this is much more of a learning exercise for me rather than something I’m trying to setup to actually do something. My goal for this was to do everything from the device without needing any HA Automations .

So with that in mind, what are the potential pitfalls of calling the HA Service from a device?

I know ESPHome has it’s own Light Component. I did try to implement this using that but the bulb I have doesn’t support setting the brightness by percentage as the light.dim_relative Action needs. I tried setting up a Template to convert the brightness level to something the bulb can understand but couldn’t get it to work.
Delete

on_release:
  - condition:
    - light.is.off: yourlight
    then:
      - homeassistant.service:
          ......blablabla

Thanks Tom. I think I’m pretty close with your suggestion. I did try several variations of this yesterday with no success. My guess is I don’t have something indented correctly or there’s missing/unneeded ‘-’ in there that’s confusing things. As a side note, can someone explain when a ‘-’ should/shouldn’t be used?

This bit returns the error shown after. What I’m trying here is to first check to see if the light is on. If not, turn it on then continue with setting the brightness. If the light is already on, just set the brightness.

  - platform: remote_receiver
    name: "Wiz Light Brighter"
#   Remote Button '+'
    internal: true
    on_release:
      - condition:
        - light.is_off: light.wiz_test_light
        then:
          light.turn_on: light.wiz_test_light
      homeassistant.service:
        service: light.turn_on
        data:
          entity_id: light.wiz_test_light
        data_template:
          brightness: '{{states.light.wiz_test_light.attributes.brightness + 10}}'
    filters:
      - delayed_off: 100ms
    pioneer:
      rc_code_1: 0x0009
INFO Reading configuration /config/esphome/ir-receiver.yaml...
ERROR Error while reading config: Invalid YAML syntax:

while parsing a block collection
  in "/config/esphome/ir-receiver.yaml", line 105, column 7:
          - condition:
          ^
expected <block end>, but found '?'
  in "/config/esphome/ir-receiver.yaml", line 109, column 7:
          homeassistant.service:
          ^

I’ve tried moving the indentation around with no luck.

The ESPHome docs show using an ‘if’ statement combined with the ‘condition’ statement. I tried several variations of that with no luck.

dictionary:
  - action 1: wtf
  - action 2:
    - wtf1
    - wtf2
  - action 3

sorry i did mistakes.and i wrote it from my head. i thought you could correct by yourself.the code should be:

on_release:
  then: # <------on release then
    - if:  #<---------action 1
        condition:  
          - light.is_off: light.wiz_test_light #<-------- check if condition is true------>
        then: # <------------- condition then
          - light.turn_on: light.wiz_test_light   # if true do it
        # - add another action
        # - add another action
    - homeassistant.service: # <---------action 2 add a - !!!!!###############
        service: light.turn_on #<--------------hassio service data
        data:   
          entity_id: light.wiz_test_light
       #  blablablabla........
   #- action3: 
     #  then:
      #   - blablabla
 #        - blablabla

Automations and Templates — ESPHome