I am using a Wemos D1 mini to sent my P1 Meter readings through MQTT to HA. This works like a charm for Electricity. However, when it comes to gas usage, the P1 only sents the total amount of gas that is used.
I would like to read the incremental values in a graph, to find out where most gas is used (eg showering, heating).
So fat I manged to do so by using automation to read the triggers from_state and its current state.
Code (hope I get it right in this post)
# Check previous reading
- alias: "Get Gas usage Delta"
trigger:
- platform: state
entity_id: sensor.p1_gas_usage
action:
- service: var.set
# value_template: {{ (((trigger.to_state.state|float) - (trigger.from_state.state|float)) *1000) | round(2) }}
data_template:
entity_id: var.diff_gas_usage
value: 1 # {{ (((states('trigger.to_state') | float) - (states('trigger.from_state') | float)) *1000) | round(2) }}
- service: notify.notify
data_template:
title: 'Gas Usage:'
message: >-
Momentarely Gas Usage {{ (((trigger.to_state.state|float) - (trigger.from_state.state|float)) *1000) | round(2) }}
The good news is, the automation above is able to sent a message with the amount of gas used. That’s nice, but not enough, I would like to have a graph.
So I used the “var” integration to define a variable:
var:
diff_gas_usage:
friendly_name: "Diff Gas Usage"
unit_of_measurement: "dm3"
initial_value: 0
icon: mdi:counter
This works, I have a graph, I can manipulate it from the automation, settings its value form 0 to 1 vv.
Only ane thing is not working, the formula used in the message is not working to assign its value to the variable (diff_gas_usage). I already used value_template: instead of value: but config check doesn’t accept it.
Anyone an idea what I’m missing to set the value?
I know that the state does not change to zero at the moment, due to there is no state change, probably will sove this later with mqtt topic update time, or set it back to zero directly after.