Custom Sensor doesn't trigger properly

Dear all,

I’ve got a TP-Link HS110 connected to my Hassio install and set up a sensor to show the amount of Watt:

- platform: template
  sensors:
    retro_watts:
      value_template: '{{ states.switch.attick_retro.attributes["current_power_w"] | replace(" W", "") | float }}'
      unit_of_measurement: 'W'

That part is working perfectly, but I want to a trigger that turns off the HS110 if it’s below 12 Watt for 30 seconds. That part doesn’t work. It sometimes triggers, but most of the time it doesn’t:

- id: '1540494169288'
  alias: Turn off Switch for Retro
  trigger:
  - platform: numeric_state
    entity_id: sensor.retro_watts
    below: '12'
    for:
      seconds: 30
  condition:
  - condition: state
    entity_id: switch.attick_retro
    state: 'on'
  - condition: state
    entity_id: media_player.sony_XXXX
    state: 'off'
  action:
  - service: switch.turn_off
    entity_id: switch.attick_retro

Could someone help me figure out what I’m missing? Thanks in advance for all the help!

try:

below: 12

if you quote 12 it is seen as string, while you want it to be a number

Aha, I didn’t notice that. Thanks.

Actually that’s not correct. The string will be converted to float, so that part is not the problem. Although I agree I wouldn’t use the quotes here anyway.

@DeMeester, the way a numeric_state trigger works when the below condition is specified is, at startup if the condition (i.e., below 12) is met, then it will trigger. Of course, with the “for” option it would then have to stay below that value for that amount of time for the trigger to actually completely fire (i.e., for the action to run, assuming of course, the automation’s conditions are met at that time.)

After that, in order for it to trigger again, the below condition has to become false (i.e., 12 or above), and then true again (i.e., below 12), and then stay true for the specified amount of time, for the trigger to fire again.

So, do you think the state of the entity is behaving that way and it should be triggering? Is it possible one of the automation’s conditions are keeping the action from running? E.g., is the state of media_player.sony_XXXX actually ‘off’ at the time the trigger fires?

BTW, using the template sensor for the trigger should be fine, but FYI, you don’t need an intermediary like that for this numeric_state trigger. You could write it this way if you wanted:

  trigger:
  - platform: numeric_state
    entity_id: switch.attick_retro
    value_template: "{{ state.attributes.current_power_w|replace(' W', '') }}"
    below: 12
    for:
      seconds: 30

And one last comment while we’re at it. In your template sensor you don’t need the final float filter. Yes, that will change the string to a number, but being a template, it will convert the result back to a string anyway.

I stand corrected… pardon me. Even so, if I don’t want to use strings, I don’t quote, even thought they might be converted to a number anyways… better be safe, and always look at the doc’s.

I think you’re right on the money about the ‘media_player.sony_XXXX’ entry. I forgot I had that set to off.

So if I set my sensor up like this it should be enough? :

- platform: template
  sensors:
    retro_watts:
      value_template: '{{ states.switch.attick_retro.attributes["current_power_w"] | replace(" W", "") }}'
      unit_of_measurement: 'W'

Yes, but I would write it using the state_attr function:

- platform: template
  sensors:
    retro_watts:
      value_template: "{{ state_attr('switch.attick_retro', 'current_power_w') | replace(' W', '') }}"
      unit_of_measurement: 'W'