rodw
(Rod Webster)
November 25, 2023, 7:36am
1
I want to build a ESP32 based device that measures flow and pressure of some sprinklers so I can calibrate a fertilizer injector. I am quite stumped decoding the JSON despite being able to do it in the template editor.
This is the JSON I settled on.
{
"sprinkler": [
{
"id": "1",
"flow": "13.6",
"pressure": "280"
},
{
"id": "2",
"flow": "15.8",
"pressure": "260"
}
]
}
And this is what I did in the template ediitor:
{% set my_sprinklers ={
"sprinklers":[
{ "id": "1", "flow": "13.6", "pressure": "280" },
{ "id": "2", "flow": "15.8", "pressure": "260" }
]
} %}
sprinkler 1 = {{ my_sprinklers.sprinklers[0] }}
sprinkler 2 = {{ my_sprinklers.sprinklers[1] }}
sprinkler 1 id = {{ my_sprinklers.sprinklers[0].id }}
sprinkler 1 flow = {{ my_sprinklers.sprinklers[0].flow }} l/min
sprinkler 1 pressure = {{ my_sprinklers.sprinklers[0].pressure}} kPa
sprinkler 2 id = {{ my_sprinklers.sprinklers[1].id }}
sprinkler 2 flow = {{ my_sprinklers.sprinklers[1].flow }} l/min
sprinkler 2 pressure = {{ my_sprinklers.sprinklers[1].pressure}} kPa
I just don’t understand you to set these to values in home assistant. Any help or guidance would be appreciated. I can reformat the JSON packet as this is going to be built from scratch if there is an easier format to decode.
tom_l
November 25, 2023, 8:16am
2
See:
e.g. in configuration.yaml
mqtt:
sensor:
- name: "Sprinkler 1 Flow"
state_topic: "your/topic/here"
value_template: "{{ my_sprinklers.sprinklers[0].flow }}"
unit_of_measurement: L/min
icon: mdi:waves-arrow-right
- name: "Sprinkler 1 Pressure"
state_topic: "your/topic/here"
value_template: "{{ my_sprinklers.sprinklers[0].pressure }}"
device_class: pressure
unit_of_measurement: kPa
- name: "Sprinkler 2 etc...
There’s no device class for flow but there may be in future so best to use the correct unit (capital L for litres and ideally you would send L/s).
You can pick any icon you want from here: Material Design Icons - Icon Library - Pictogrammers some of the newer ones may not be added to HA yet.
rodw
(Rod Webster)
November 25, 2023, 12:43pm
3
Thanks for your help. Really helpful. I ended up with this
mqtt:
sensor:
- name: "Sprinkler 1 Flow"
state_topic: "homeassistant/sprinklers"
value_template: "{{ sprinklers[0].flow }}"
unit_of_measurement: L/min
icon: mdi:sprinkler
- name: "Sprinkler 1 Pressure"
state_topic: "homeassistant/sprinklers"
value_template: "{{ sprinklers[0].pressure }}"
device_class: pressure
unit_of_measurement: kPa
icon: mdi:gauge
Which creates the sensors but does not create any errors but I am getting values of unknown . Do I need to use a value format?
tom_l
November 25, 2023, 1:18pm
4
Oops. I missed replacing an important part of the templates:
mqtt:
sensor:
- name: "Sprinkler 1 Flow"
state_topic: "homeassistant/sprinklers"
value_template: "{{ value_json.sprinklers[0].flow }}"
unit_of_measurement: L/min
icon: mdi:sprinkler
- name: "Sprinkler 1 Pressure"
state_topic: "homeassistant/sprinklers"
value_template: "{{ value_json.sprinklers[0].pressure }}"
device_class: pressure
unit_of_measurement: kPa
icon: mdi:gauge
rodw
(Rod Webster)
November 25, 2023, 9:41pm
5
Got it! but it seems HA does not like the array, I had to change JSON to
{
"sprinkler1": {
"flow": 11.11,
"pressure": 222
},
"sprinkler2": {
"flow": 12.22,
"pressure": 333
}
}
Then it worked!
mqtt:
sensor:
- name: "Sprinkler 1 Flow"
state_topic: "homeassistant/sprinklers"
value_template: "{{ value_json.sprinkler1.flow }}"
unit_of_measurement: L/min
icon: mdi:sprinkler
I also tested this with mosquito_pub as working
mosquitto_pub -V mqttv311 -u homeassistant -P Its_A_Secret -h 192.168.1.29 -p 1883 -m '{"sprinkler1":{"flow":15.11,"pressure":252},"sprinkler2":{"flow":16.66,"pressure":444}}' -t "homeassistant/sprinklers"
Thanks so much for your help.
Now I have HA set up on to some ESP32 code and some plumbing when the sensors turn up.