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:
,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.
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.
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'
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.
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!!!
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?
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)
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:
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 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!