You don’t really give enough information on how the value is received,
I’m not really sure how it is received. It is sent with Arduino code client.publish(“HotWaterPercent”, chrBuffer); where chrBuffer holds the value. I’m not sure what mqtt does with it as such other than i can read it.
ok i put the below in config.yaml but it objects when i reload but the error is too long and disappears before i can read it. something about unexpected " @16 for dictionary value
Do you have the mqtt integration installed?
It actually has a way to listen to topics and you could investigate the payload from there and create your sensor when you confirm HA is seeing it.
I changed the Arduino to send json as this seemed like the best way to go.
MQTT registers the value and if i create a card n the dashboard i can see the posted value. In my template i put value_template: “{{ value_json.Value | int }}” but i note that my card displays the value in quotes, and any maths comparisons i try to do in my automation do not work.
I want to test if the value is lower than a fixed number before i take an action. Have i gone about this the right way ?
type: toggle
device_id: 68b758a83f130fc9a62c32e75bc7c1c5
entity_id: d8abcb7886e3cfb867e12e392b3ec168
domain: switch
mode: single
If as you say mqtt values are always strings, then isn’t that why the | int is there when creating the entity, i thought the entity was ‘outside’ of mqtt created on arrival as specified in config and could be an int ?
So do i need to convert to int at the point of comparison ? Using jinja in the link you shared ? Is it a problem of type conversion ?
I’m getting confused here: you are still using an MQTT trigger in the automation but were trying to set up a sensor before. Please decide if you want to set up a sensor (recommended) or use the MQTT trigger in the automation.
Automation numeric_state conditions will do the string-to-number conversion for you, so you don’t need to worry about that provided the incoming string can be converted. It’s only in Jinja templates that you need to do the conversion yourswel
My trigger is arrival of the mqtt data, i am thinking (but could be wrong) this is converted to an entity which i can do the comparison on. As there will later be a time element i.e. only check at 4pm, i might be better using time as the trigger and then simply checking the entity which i assume will hold the last reading.
That sounds more efficient but I still have the issue on the number comparison in the entity - i’ll look into numeric_state
Unless you have MQTT discovery set up, there is no entity created from the MQTT data. You will need to create one, which will look a bit like the code in your post #5 above.
You can then use that entity in multiple places: dashboard display, automation triggers etc.
Once that’s sorted and visible in Developer Tools / States, we can help with the automation. You might want more than just a time trigger: what if the “level” drops below 40% at 4:05pm?
You’re only limited by your imagination once you have the input data coming in and the ability to control going out. I have a similar setup and a schedule of hot water targets depending on time of day, different at weekends:
Ha great minds think alike. 5 sensors is itneresting as i realise that the immersion can only ever heat to 80% as its not at the bottom of the tank. I log all this into Influx and plot with Grafana. I have been sending the % full value to Apilio, and getting that to do the caompare at 4pm, and then it makes a web call to start the boiler via IFTT. All a bit convoluted hence the HA interest.
So i have the code as in psot 5 and it seems to create an entity called sensor.hotwaterfull which i can select in the conditions section, and it results in the code above compare < 40. However it doesn’t work as intended and i wonder now based on what you said above if i need to convert the entity to numeric when i do the compare. Seems like the entity is a string deduced by the fact it is in quotes on the card i created and your values aren’t above.
when you manually run an automation, only the action is run. Conditions are ignored.
However, your sensor state appears to have quotation marks around it: "40" rather than 40:
You need to remove those or the numeric_state comparison will fail. Please post the sensor config code and I’ll advise. It’ll be adding something like:
yes i think the quoatation marks are the issue. In my lovelace card the value also appears in quotes. The sensor is created with the code in config.yaml below, and i thought the | int would ensure it was stored as a number i could then do math on but it doesn’t seem to be doing that,
Those are “smart quotes”, which, as far as the system goes, aren’t quotes at all, and are being included in your sensor state.
Should be (look carefully, and copy and paste this):
value_template: "{{ value_json.Value | int }}"
I would imagine this has arisen from someone not following the formatting guidelines, then you copying out incorrectly-formatted code into your configuration.
that works - lovelace card now has no quotes and trigger works as intended
thank you very much for your help, the perils of cut and paste but the distinction between quotes and smart quotes is subtle, i could barely see the difference even when pointed out.