to send data to my mqtt broker. Unfortunately sometimes the script that is reading data from my RCT Inverter fails to read data.
So my automation is putting “unavaible” as payload to the mqtt topic.
I dont want this. Is there a chance to send nothing if reading the data fails?
I only want to have valid data on my topic. In this case only numbers no text.
How to do this?
Add a Template Condition that checks if the sensor has a nominal value. It it’s not a nominal value, such as unavailable or unknown, the condition will prevent the automation from executing its action.
alias: RCT Power Storage All Generators Power via MQTT
description: ""
trigger:
- platform: state
entity_id: sensor.rct_power_storage_all_generators_power
- platform: time_pattern
seconds: /15
condition:
- condition: template
value_template: "{{ has_value('sensor.rct_power_storage_all_generators_power') }}"
action:
- service: mqtt.publish
data:
topic: rct_power_storage/generator/power
payload_template: "{{ states('sensor.rct_power_storage_all_generators_power') }}"
mode: single
If you absolutely want it to publish “nothing” when the value is unavailable then you can use this to publish an empty string.
alias: RCT Power Storage All Generators Power via MQTT
description: ""
trigger:
- platform: state
entity_id: sensor.rct_power_storage_all_generators_power
- platform: time_pattern
seconds: /15
condition: []
action:
- service: mqtt.publish
data:
topic: rct_power_storage/generator/power
payload_template: >
{% set s = 'sensor.rct_power_storage_all_generators_power' %}
{{ states(s) if has_value(s) else '' }}
mode: single
Thanks a lot. That works. But now i found another problem: When my AC Inverter ist disconnected Home Assistant is now publishing the last value every 15seconds to my mqtt broker.
In this case i need Home Assistant stop publishing.
Or in another words: if the value is older than lets say 30seconds, dont publish.
When it’s disconnected, what is the value of sensor.rct_power_storage_all_generators_power?
If the value is unavailable, or unknown, use the first example I posted above. It’s designed to avoid publishing any value when the sensor’s value is unavailable.
Two question for you:
Why is there a need to publish a value every 15 seconds?
Wouldn’t it be sufficient to only publish the sensor’s value when it changes?
im using this mqtt topics for evcc. evcc goes in error mode then there is no value received for more than 30sec. So i want to make sure that evcc has data that has a maximum age of 30sec.
When i disconnect the AC Inverter sensor.rct_power_storage_all_generators_power is keeping the last value.
i dont see these reading errors in HomeAssistant, but when i check the topics with mqtt explorer from time to time there is “unavaible”
Are you saying that under normal conditions, when the AC inverter is connected, the sensor’s last value is never the same as its current value? In other words, it’s always changing and no two consecutive values are ever the same?
Because if it’s possible for the value to be unchanged under normal conditions then it can’t be used as a sign of AC inverter disconnection.
That would imply the sensor’s value is unavailable.