Using wemo state_detail in an automation

Hey guys I’m new to home assistant and I’ve been really trying to work this out for myself but I think I just need a little help doing my first automation with templates so I can reference back to it in the future.

I’m trying to automate my washing mashing to notify me when it is done i tried doing a basic if the power falls below 4w for 3 minutes but I got lots of nuisance triggering when the wemo would drop of the network for a second.

I’ve noticed the wemo has a ‘state_detail’ which includes

‘off’ switch turned off
‘standby’ switch on but not drawing power
‘on’ switch on and consuming power

I want my automation to detect a change from the ‘on’ state to the ‘standby’ state only after 3 minutes

I setup my sensor in configuration. yaml

sensor:
#wemo
  - platform: template
    sensors:
      washing_machine_state:
        value_template: '{{- states.switch.washing_machine.attributes.state_detail }}'
        friendly_name: 'washing machine state'

this seems to work now I can’t get the correct formatting for my automation

  - alias: Washer cycle complete
    trigger:
      platform: state
      entity_id: switch.washing_machine
      value_template: >-
{{ states.switch.washing_machine.attributes.state_detail == 'on' }}
    washing
{{ states.switch.washing_machine.attributes.state_detail == 'standby' }}
    standby
{{ states.switch.washing_machine.attributes.state_detail == 'off' }}
    off

I’ve managed to get the template maker to recognise the 3 states with a true or false but I just need help incorporating it into an automation that basically says "if the state_detail changes from ‘on’ to ‘standby’ for 3 minutes run the automation any other combination of state change im not interested in.

Really sorry to have to ask guys but it’s really got the better of me. Just need it explained to me in laymans terms so I can do it myself in future.

cheers

For a template trigger, like you’re trying to use, it needs to evaluate to true when you want it to trigger, and something else when it shouldn’t trigger. Note also that it will trigger the first time it evaluates to true, and then after that, it has to evaluate to something other than true, and then back to true for it to trigger again.

But your template is evaluating to ‘washing’, ‘standby’ or ‘off’, and never to ‘true’, so it will never trigger.

Actually, I think the easiest way to do this, especially since you want the “for 3 minutes” condition, and you already have a template sensor whose state is the state_detail attribute, is to use that template sensor in the trigger. But first you should probably get rid of that stray - character. And you should use the state_attr function when possible.

So putting it all together:

sensor:
#wemo
  - platform: template
    sensors:
      washing_machine_state:
        value_template: "{{ state_attr('switch.washing_machine', 'state_detail') }}"
        friendly_name: "washing machine state"
automation:
  - alias: Washer cycle complete
    trigger:
      platform: state
      entity_id: sensor.washing_machine_state
      from: 'on'
      to: 'standby'
      for:
        minutes: 3
    action:
      ...

This will trigger when switch.washing_machine’s state_detail attribute changes from “on” to “standby”, and stays “standby” for 3 minutes.

1 Like

Thanks so much for your reply, I’ll try it out when I get home and let you know how I get on.

Thanks mate that worked perfectly and sent a notification to my phone via pushbullet. Hopefully now that i can see how to create a sensor and integrate it into an automation I’ll be able to do it myself next time :slight_smile:

thanks again

I’m also trying to create a binary sensor from the state_details for a sump pump that I use on a Wemo Insight but am not having much luck. The notification example above for the washing machine is a good notification solution, but I also want to create a visual cue on a card in the HASS Interface and thought a binary sensor would be an easy way to implement that.

sump_pump.yaml

platform: template
sensors:
  sump_pump_on:
  friendly_name: "Sump Pump On"
    value_template: {{ states.switch.sump_pump.attributes.state_detail == 'on' }}

What am I missing?

Thanks,
Pete

A few things. First, where is sump_pump.yaml, and how is it being included in the overall configuration? That will affect whether platform needs a dash before it, and also if there needs to be a binary_sensor: line before it.

Next, friendly_name needs to be indented under sump_pump_on (at the same indentation level as value_template.)

Next, you need to put quotes around the template.

Lastly, it’s better to use the is_state_attr function.

So, at the least it needs to be:

platform: template
sensors:
  sump_pump_on:
    friendly_name: "Sump Pump On"
    value_template: "{{ states.switch.sump_pump.attributes.state_detail == 'on' }}"

Or better yet:

platform: template
sensors:
  sump_pump_on:
    friendly_name: "Sump Pump On"
    value_template: "{{ is_state_attr('switch.sump_pump','state_detail','on') }}"

But possibly:

- platform: template
  sensors:
    sump_pump_on:
      friendly_name: "Sump Pump On"
      value_template: "{{ is_state_attr('switch.sump_pump','state_detail','on') }}"

or even:

binary_sensor:
  - platform: template
    sensors:
      sump_pump_on:
        friendly_name: "Sump Pump On"
        value_template: "{{ is_state_attr('switch.sump_pump','state_detail','on') }}"

Thank you. I went with your last option (which i thought I had tried) which works flawlessly.

(configuration.yaml)
binary_sensor: !include_dir_list binary_sensors

(Sub Dir - binary_sensors)

(sump_pump.yaml)
    platform: template
    sensors:
      sump_pump_on:
        friendly_name: "Sump Pump On"
        value_template: "{{ is_states_attr('switch.sump_pump','state_detail','on') }}"

I’ll now try to add an alternate icon with the following:

platform: template
sensors:
  sump_pump_on:
    friendly_name: "Sump Pump On"
    value_template: "{{ is_states_attr('switch.sump_pump','state_detail','on') }}"
    icon_template: >-
      {{ if is_states_attr('switch.sump_pump','state_detail','on') }}
        mdi:flash
      {% else %}
        mdi:flash-off
      {% endif %}

Thanks Again!

Didn’t like that… :frowning:

2019-01-18 14:46:55 ERROR (MainThread) [homeassistant.config] Invalid config for [binary_sensor.template]: invalid template (TemplateSyntaxError: expected token ‘end of print statement’, got ‘is_states_attr’) for dictionary value @ data[‘sensors’][‘sump_pump_on’][‘icon_template’]. Got “{{if is_states_attr(‘switch.sump_pump’,‘state_detail’,‘on’) }}\n mdi:flash\n{% else %}\n mdi:flash-off\n{% endif %}”
invalid template (TemplateSyntaxError: expected token ‘end of print statement’, got ‘is_states_attr’) for dictionary value @ data[‘sensors’][‘sump_pump_on’][‘value_template’]. Got “{{ if is_states_attr(‘switch.sump_pump’,‘state_detail’,‘on’) }}”. (See ?, line ?). Please check the docs at https://home-assistant.io/components/binary_sensor.template/

Oops, my typo. The function is is_state_attr. Sorry about that! (I’ll go back and fix my previous post.)

All works well now… :slight_smile:

Thank you!

platform: template
sensors:
  sump_pump:
    friendly_name: "Sump Pump"
    value_template: "{{ is_state_attr('switch.sump_pump','state_detail','on') }}"
    icon_template: >-
      {% if is_state_attr('switch.sump_pump','state_detail','on') %}
        mdi:flash
      {% else %}
        mdi:flash-off
      {% endif %}
1 Like