Automation triggers due to unavailable state

Hi @all,

i am very new at HA and i’m getting a little stuck with my config.

I am using HA 0.99.3 within Docker on my Synology and everything is working fine but not my two tplink switches HS110. They are from time to time unavailable. This causes my automation to send me a notification.

I would like to get informed when my washing mashine and my tumble dryer finishes its work. This is my config and my automation:

Config:

tplink:
  discovery: false
  switch:
    - host: 192.168.2.79
    - host: 192.168.2.80

sensor:
  - platform: template
    sensors:
      waschmaschine_amps:
        friendly_name_template: "{{ states.switch.waschmaschine.name}} Ampere"
        value_template: '{{ states.switch.waschmaschine.attributes["current_a"] | float }}'
        unit_of_measurement: 'A'
      waschmaschine_watts:
        friendly_name_template: "{{ states.switch.waschmaschine.name}} Aktueller Verbrauch"
        value_template: '{{ states.switch.waschmaschine.attributes["current_power_w"] | float }}'
        unit_of_measurement: 'W'
      waschmaschine_total_kwh:
        friendly_name_template: "{{ states.switch.waschmaschine.name}} Gesamter Verbrauch"
        value_template: '{{ states.switch.waschmaschine.attributes["total_energy_kwh"] | float }}'
        unit_of_measurement: 'kWh'
      waschmaschine_volts:
        friendly_name_template: "{{ states.switch.waschmaschine.name}} Spannung"
        value_template: '{{ states.switch.waschmaschine.attributes["voltage"] | float }}'
        unit_of_measurement: 'V'
      waschmaschine_today_kwh:
        friendly_name_template: "{{ states.switch.waschmaschine.name}} Heutiger Verbrauch"
        value_template: '{{ states.switch.waschmaschine.attributes["today_energy_kwh"] | float }}'
        unit_of_measurement: 'kWh'
      trockner_amps:
        friendly_name_template: "{{ states.switch.trockner.name}} Ampere"
        value_template: '{{ states.switch.trockner.attributes["current_a"] | float }}'
        unit_of_measurement: 'A'
      trockner_watts:
        friendly_name_template: "{{ states.switch.trockner.name}} Aktueller Verbrauch"
        value_template: '{{ states.switch.trockner.attributes["current_power_w"] | float }}'
        unit_of_measurement: 'W'
      trockner_total_kwh:
        friendly_name_template: "{{ states.switch.trockner.name}} Gesamter Verbrauch"
        value_template: '{{ states.switch.trockner.attributes["total_energy_kwh"] | float }}'
        unit_of_measurement: 'kWh'
      trockner_volts:
        friendly_name_template: "{{ states.switch.trockner.name}} Spannung"
        value_template: '{{ states.switch.trockner.attributes["voltage"] | float }}'
        unit_of_measurement: 'V'
      trockner_today_kwh:
        friendly_name_template: "{{ states.switch.trockner.name}} Heutiger Verbrauch"
        value_template: '{{ states.switch.trockner.attributes["today_energy_kwh"] | float }}'
        unit_of_measurement: 'kWh'type or paste code here

Automations:

- id: '1567969741240'
  alias: Waschmaschine fertig
  trigger:
  - below: '6.0'
    entity_id: sensor.waschmaschine_watts
    for: 00:00:20
    platform: numeric_state
  condition: []
  action:
  - data:
      message: Waschmaschine FERTIG!
    service: notify.ios_lucky
- id: '1567970943938'
  alias: Waschmaschine an
  trigger:
  - above: '6.0'
    entity_id: sensor.waschmaschine_watts
    for: 00:00:20
    platform: numeric_state
  condition: []
  action:
  - data:
      message: Waschmaschine STARTET!
    service: notify.ios_lucky
- id: '1568721877770'
  alias: Trockner An
  trigger:
  - above: '5'
    entity_id: sensor.trockner_watts
    for: 00:01:00
    platform: numeric_state
  condition: []
  action:
  - data:
      message: Trockner STARTET!
    service: notify.ios_lucky
- id: '1568721993054'
  alias: Trockner Fertig
  trigger:
  - below: '5.0'
    entity_id: sensor.trockner_watts
    platform: numeric_state
  condition: []
  action:
  - data:
      message: Trockner FERTIG!
    service: notify.ios_lucky

Errors i found in the logs:

,2019-10-07 11:20:34 WARNING (SyncWorker_0) [homeassistant.components.tplink.switch] Could not read state for 192.168.2.79: Communication error
,2019-10-07 11:20:34 ERROR (MainThread) [homeassistant.components.template.sensor] Could not render template Waschmaschine Heutiger Verbrauch: UndefinedError: 'mappingproxy object' has no attribute 'today_energy_kwh'
,2019-10-07 11:20:34 ERROR (MainThread) [homeassistant.components.template.sensor] Could not render template Waschmaschine Ampere: UndefinedError: 'mappingproxy object' has no attribute 'current_a'
,2019-10-07 11:20:34 ERROR (MainThread) [homeassistant.components.template.sensor] Could not render template Waschmaschine Aktueller Verbrauch: UndefinedError: 'mappingproxy object' has no attribute 'current_power_w'
,2019-10-07 11:20:34 ERROR (MainThread) [homeassistant.components.template.sensor] Could not render template Waschmaschine Gesamter Verbrauch: UndefinedError: 'mappingproxy object' has no attribute 'total_energy_kwh'
,2019-10-07 11:20:34 ERROR (MainThread) [homeassistant.components.template.sensor] Could not render template Waschmaschine Spannung: UndefinedError: 'mappingproxy object' has no attribute 'voltage'

Does anybody know how to fix this?

I already tried to change the wifi connection (channel) of the tplink switches and also the position of them. But nothing worked.

Thanks a lot.

Lucky

It depends on what you mean by “fix it.”

Of course the best way to fix the situation is to fix the unreliability of the devices themselves so they always have valid state in HA.

But if you would just like HA to effectively ignore when the devices are unavailable (and have the corresponding template sensors keep their previous values until new valid data is received), you can do that like the following:

      waschmaschine_amps:
        friendly_name_template: "{{ states.switch.waschmaschine.name}} Ampere"
        value_template: >
          {% set value = state_attr('switch.waschmaschine', 'current_a') %}
          {{ value|float if value is not none else states('sensor.waschmaschine_amps') }}
        unit_of_measurement: 'A'

If either switch.waschmaschine doesn’t exist, or it does exist but it doesn’t have an attribute named current_a, then the state_attr() function will return the value none. In that case value_template will evaluate to the current state of sensor.waschmaschine_amps (which would have been set the last time state_attr('switch.waschmaschine', 'current_a') returned a valid value.)

EDIT: As noticed below I had put the float filter in the wrong place. Fixed above for future reference.

Good morning. Thank you for you fast reply. Communities are really awesome. A lot of awesome people with cool ideas!!!

Of course it would be better to fix the problem by the root cause but in this case i am also happy if the “workaround” works. I will try your setting and give you later feedback.

Ok so now i get following message in the logs

Value cannot be processed as a number: <state sensor.trockner_watts=None; unit_of_measurement=W, friendly_name=Trockner Aktueller Verbrauch, icon=mdi:tumble-dryer @ 2019-10-08T09:40:21.644859+02:00> (Offending entity: None)

And my automation still is getting fired a push notification

Config looks like this:

      trockner_watts:
        friendly_name_template: "{{ states.switch.trockner.name}} Aktueller Verbrauch"
        value_template: >
          {% set value = state_attr('switch.trockner', 'current_power_w') %}
          {{ value if value|float is not none else states('sensor.trockner_watts') }}
       # value_template: '{{ states.switch.trockner.attributes["current_power_w"] | float }}'
        unit_of_measurement: 'W'

Oops, my mistake. I put the float filter in the wrong place. Try this:

      trockner_watts:
        friendly_name_template: "{{ states.switch.trockner.name}} Aktueller Verbrauch"
        value_template: >
          {% set value = state_attr('switch.trockner', 'current_power_w') %}
          {{ value|float if value is not none else states('sensor.trockner_watts') }}
       # value_template: '{{ states.switch.trockner.attributes["current_power_w"] | float }}'
        unit_of_measurement: 'W'
1 Like

No problem. I changed it yesterday after you told me. And i think it works for me. But i will wait until the end of the day and test it better. Then i will inform you. Thanks a lot.

1 Like

So this worked for me. I still see the communication warning in the logs but no more errors and my automation works like it should do. Thanks a lot. You are awesome!!!

1 Like

I have similar issue

Could not render template Bedroom Today’s Consumption: UndefinedError: ‘mappingproxy object’ has no attribute ‘today_energy_kwh’

10:12 PM components/template/sensor.py (ERROR) - message first occurred at 10:12 PM and shows up 2 times

Could not render template Bedroom Voltage: UndefinedError: ‘mappingproxy object’ has no attribute ‘voltage’

10:12 PM components/template/sensor.py (ERROR) - message first occurred at 10:12 PM and shows up 2 times

Could not render template Bedroom Total Consumption: UndefinedError: ‘mappingproxy object’ has no attribute ‘total_energy_kwh’

10:12 PM components/template/sensor.py (ERROR) - message first occurred at 10:12 PM and shows up 2 times

Could not render template Bedroom Current Consumption: UndefinedError: ‘mappingproxy object’ has no attribute ‘current_power_w’

10:12 PM components/template/sensor.py (ERROR) - message first occurred at 10:12 PM and shows up 2 times

Could not render template Bedroom Current: UndefinedError: ‘mappingproxy object’ has no attribute ‘current_a’

10:12 PM components/template/sensor.py (ERROR) - message first occurred at 10:12 PM and shows up 2 times

Could not render template bedroom_today_kwh: UndefinedError: ‘mappingproxy object’ has no attribute ‘today_energy_kwh’

9:33 PM components/template/sensor.py (ERROR)

Could not render template bedroom_volts: UndefinedError: ‘mappingproxy object’ has no attribute ‘voltage’

I run
Home Assistant 0.101.3
HassOS 3.5
Version 192

Am I doing something wrong or it just HA version thing ?

I have done nothing else then this TP-Link Smart Home - Home Assistant

Any ideas ?
Thanks

Can you please show your code for the sensors?

I think it is the same issue like i have (same HA version). I still have the “ERROR” entry in my log but the “mappingproxy” is gone with the solution above. Even if there is this error everything works for me. Do you face some problems with the plugs?

Sorry its my bad. I found this configuration

- platform: template
    sensors:
      bedroom_amps:
        friendly_name_template: "{{ states.switch.bedroom.name}} Current"
        value_template: >
          {% set value = state_attr('switch.bedroom', 'current_a') | float %}
          {{ value }}
        unit_of_measurement: 'A'

No errors with above. I but got 0 values because my plugs don’t support energy monitoring
HS105
HS200

Why do you create the sensor if your plugs don’t support energy monitoring??

I was not aware of it.

Just remove the sensors as you don’t need them and then your errors will disappear.

I took a different (or similar depending onn how you look at it :-)) approach: I added a template condition to the automation that looks like the below (this should prevent the automation from triggering if the values are off like unavailable or negative)

{{ states('sensor.energy_washing_machine') | float + 1 > 0 }}

You might want to check again. The float filter will output (by default) zero if its input (in this case, the state of sensor.energy_washing_machine, or 'unknown' if the entity doesn’t exist) cannot be interpreted as a number, which will make the expression true. Maybe you might try:

{{ states('sensor.energy_washing_machine') | float(-1) + 1 > 0 }}

That changes the float filter’s output to minus one if the input cannot be interpreted as a number, making the expression evaluate to false.

Thanks! That indeed did the trick!

Hmmm… I still get notifications when the state is unavailable. Giving it some additional thought: that might be normal as the state goes to ‘unknown’ and after that back to zero (which triggers the automation again).

I could now add an automation that only enables the automation when the power is above a certain value, but that would be a bit of a dirty trick. Any other suggestions?

I had exactly the same issue, that I get a notification when the state goes from “unavailable” to “0”.

I made a condition for the automation:

- condition: template
    value_template: '{{ trigger.from_state.state != "unavailable" }}'

Maybe this is also a solution for your problem.

1 Like

I was still experiencing some random triggers, so I now opted for the “{{ states(‘sensor.energy_washing_machine’) != “unavailable” }}” solution. Let’s see if that remains robust. Thanks!