Automation to turn off a switch

Hi all, thanks in advance for your help.

I have a SmartThings Smart Plug on which I monitor the power usage of the mains power plug where I charge all my portable electronics.

I created a templated sensor that displays a few states for the plug.

  • Turned Off
  • Low Power (when it’s using less than 20W of power)
  • Charging (when it’s using more than 20W of power)

This all works pretty well - I’m happy.

I’ve created an automation too - I’d like the switch to turn off once the templated sensor has been in the “Low Power” state for 30 mins.

- id: '9C89A2AE-71F4-4C25-957A-896F4C954212'
  alias: "Charging Station / Off"

  - entity_id: sensor.charging_station_status
    platform: state
    to: 'Low Power'
      minutes: 30

  - service: switch.turn_off
        - switch.study_charging_station

This used to work (I don’t think I’ve changed anything), but it doesn’t anymore. I’ve validated the entity names, and in Lovelace the “last changed” value of the charging status sensor is more than the required 30 mins.

I’m sure I’m missing a trick … would someone be able to please point me in the right direction?

Thank you!

paste this into Template Editor

{{ states('sensor.charging_station_status') }}

when your device is in Low power state and use it here

as I suspect it’s different so your automation doesn’t trigger.
and yes, it would be nice to see your template sensor’s config.

Thanks for your help!

From Template Editor …

Screenshot 2020-03-26 at 9.43.02 am

The sensor config is …

- platform: template
        icon_template: mdi:power-settings
        friendly_name: "Charging Station - Status"
        value_template: >-
          {% if is_state('switch.study_charging_station', 'off') %}
            Turned Off
          {% elif state_attr('switch.study_charging_station', 'current_power_w')|float < 20 %}
            Low Power
          {% else %}
          {% endif %}

Are my single quotes in the automation trigger making my “Low Power” different enough to not trigger?

The quotes just ensure the text is a string, so you’re good there. Everything looks good. I noticed you don’t have an initial state set for the automation, so is the automation on?

I don’t normally turn automations off, which was why I didn’t set an initial state, but the more I think about it the more I realise it won’t hurt. I’ll go back at some point and add that in.

Looking in Dev Tools / States, both charging station automations are on.
Apparently charging station off was last triggered 2 days ago (least it’s been triggered?!) but that doesn’t explain why it’s not triggering now.

Screenshot 2020-03-26 at 1.14.34 pm

Thanks for everyone’s continued help … :pray:t2::pray:t2::pray:t2::pray:t2:

Then I’d imagine the sensor hasn’t gone from Charging or Power Off to Low Power since the automation was last triggered.

Worked it all out - thanks for your help!

It was user error. When rebooting Home Assistant, the sensors would very briefly change to “Unknown” and would throw everything off. A few tweaks later and I’m back up and running. :grinning:

I believe it would be beneficial for the community if you could post your working config with some comments about the solution - please consider it. :wink: