Automation based on time and power consumption

Hi, I’m trying to turn off a light if the time is between 23:00:00 and 05:00:00, and the power consumption of the TV is less than 2. But I can’t get my automation to work.
What am I doing wrong here?

- alias: 'Light livingroom off night'
  trigger:
    platform: time
    minutes: '/5'
    seconds: 00
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: '{{ states.sensor.fibaro_plug_tv_power < 2 }}'
      - condition: and
        conditions:
          - condition: time
            before: '05:00:00'
            after: '23:00:00'
          - condition: state
            entity_id: light.oslo_wood
            state: 'on'
  action:
    service: light.light_off
    entity_id: light.oslo_wood
    data:
     transition: 30

EDIT: Added final automation with changes form @anon43302295 and @VDRainer for anyone else interested.

Automation: Light livingroom off night
  • alias: ‘Light livingroom off night’
    trigger:
    platform: time
    minutes: ‘/5’
    seconds: 00
    condition:
    condition: and
    conditions:
    - condition: template
    value_template: ‘{{ (states.sensor.fibaro_plug_tv_power | int) < 2 }}’
    - condition: and
    conditions:
    - condition: time
    before: ‘05:00:00’
    after: ‘23:00:00’
    - condition: state
    entity_id: light.oslo_wood
    state: ‘on’
    action:
    • service: light.turn_off
      entity_id: light.oslo_wood
      data:
      transition: 30
    • service: notify.telegram
      data:
      message: “Nighttime, and the TV is off. Turning off light…”

The condition for whether the light is on doesn’t matter, as turning it off if it already is off isn’t a problem - you can add this back in later to prevent the automation actually firing if the light is on, but for now try this and see if it works tonight…

- alias: 'Light livingroom off night'
  trigger:
    platform: time
    minutes: '/5'
    seconds: 00
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: '{{ (states.sensor.fibaro_plug_tv_power | int) < 2 }}'
      - condition: time
        before: '05:00:00'
        after: '23:00:00'
  action:
    service: light.light_off
    entity_id: light.oslo_wood
    data:
     transition: 30

If it does, then re-add the second and condition (I’ve just stripped it out for troubleshooting purposes)

1 Like

Thank you, can’t wait till tonight :slightly_smiling_face:

So, what’s different now is the int part?

Should be turn_off, or have i missed something?

1 Like

Yeah, I thought the result might be coming back in a string, so was converting it to a number so HA could do the calculation.

But @VDRainer is probably right, you’ve got the wrong service call.

Thank you @VDRainer and @anon43302295, I can’t wait to see if it works better now! :slight_smile:

It works great now, thank you both!

A side note: I added a notify so I could see that the script executed. This resulted in notifications every 5 minutes, when the script ran. I had to re-add the check for light on to fix this.