Filter negative values in MQTT Publish automation

Hi

I’m fairly new to HASS.
I’m running the Huawei Solar integration and it’s working almost perfect.
I’ve created an automation to send my current power (coming from my solar panels) and total daily yield via MQTT to Domoticz using this code:

service: mqtt.publish
data:
  topic: domoticz/in
  payload: >-
    { "command": "udevice", "idx": 728, "nvalue": 0, "svalue": "{{
    states.sensor.active_power.state }};{{ (states.sensor.daily_yield.state) |
    round(3) * 1000 }}"}

However… everyday at 6AM, the daily yield value gets reset to 0. This results in a big negative value being send to MQTT/Domoticz. Is it possible to filter out a negative value in states.sensor.daily_yield.state please?

Tnx!

You can use a template sensor.

1 Like

Ok, tnx for the very quick reply!

          {% if states('sensor.daily_yield') | float * 1000 > 0 %}
            {{ states('sensor.daily_yield') | float * 1000 }} 
          {% else -%}
            0
          {% endif %}

Seems to work to only give positive values. Tnx!
An other option seems to be to feed the total_yield instead of the daily_yield. Total yield only adds up and never gets reset to 0.

Any hint how to get the value from the template in the above service: mqtt.publish please?

I’m confused sorry. Do I give the template a name (like a variable) and use that new name in my automation or do I put the template itself in my automation? :face_with_raised_eyebrow:

If you wish, you can use the template above directly in the MQTT Publish service call (i.e. no need to create a Template Sensor unless you need it for some other purpose).