Starting the boiler if tank has less than x% hot water in it

I’m new to HA but have a long hisotry of Iot using various platforms, but finding Alexa and the rotuines somewhat limiting even when using with IFTT or Apilio.

I want to fire up the boiler to heat the water ifat 4pm the tank has less than 40% of hot water in it. I know how much water is in it as I have 5 thermometers at different levels and these report in to Influx and the percentage full figure to MQTT running on the HA machine. The value is posted as single non JSON value by an esp8266 as HotWaterPercent,50 and i can verify this is arriving ok in MQTT.

I’m trying to write an automation (not a script, but using the GUI) that triggers when a new value is received and if its after 4pm and value is less than etc etc

However, i can’t work out how to extract the value in the mqtt trigger for use in a following condition statement. Do I need to put something in payload part of the mqtt trigger ? Do i need to create an entity ?

Is this easier if i post as json, and is there an advantage when it is just one number ?

thanks

Edit: I’ve actually read your question properly now, apologies.

I’d say yes, create an entity in HA. That’s the whole idea behind the system: all your external inputs come in and get represented by entities, then the system works with them to influence outputs accordingly.

You don’t really give enough information on how the value is received, but if you’re looking to pull 50 out of HotWaterPercent,50 then use:

value_template: "{{ value.split(',')[1]|int(0) }}"

thank you for the response.

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.

value_template: “{{ value.split(‘,’)[1]|int(0) }}”

do i put that line in the payload (optional) part of the mqtt trigger, and then will that create an entity i can work with ?

No. Create an MQTT sensor:

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

Example configuration.yaml entry

mqtt:
sensor:
- name: “HotWaterPercent”
state_topic: “home/HotWaterPercent”
value_template: “{{ value.split(‘,’)[1]|int(0) }}”

i look at the documentaiton but i couldn’t find the syntax for this form.

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 removed one set of curly brackets and it seemed to work. I now have an entity call HotWaterPercent I can use in a condition statement,.

thank you for the guidance

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 ?

Again, show us the code you currently have. Entity states are always strings and need converting at the point of doing comparisons.

See the docs:

this is the code that the gui generates:-

alias: Hot Water afternoon check
description: “”
trigger:

  • platform: mqtt
    topic: /IOT/HotWaterPercent
    condition:
  • condition: numeric_state
    entity_id: sensor.hotwaterfull
    below: 40
    action:
  • 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 ?

thanks

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

We need to see in correctly-formatted code (see here for instructions how to do this):

  • the sensor configuration you’re using (if you are trying to set one up)
  • the state and attributes of that sensor from Developer Tools / States
  • the automation YAML as above but correctly formatted
  • the topic and exact message you’re looking at — so far, you’ve used both home/HotWaterPercent and /IOT/HotWaterPercent
  • any error messages or log entries

ok i think i understand what you are saying here.

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:

I have my three tank sensors reporting straight into HA from ESPHome, and the “level” calculation is done internally.

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.

Post a screenshot from Developer Tools / States showing your sensor; and the automation code properly formatted.

The automation above looks like it’s toggling a device — does that work if you run the automation manually?

yes for testing its toggling a socket and when i hit run it toggles even though the value is 40 not below 40

alias: Hot Water afternoon check
description: ""
trigger:
  - platform: mqtt
    topic: /IOT/HotWaterPercent
condition:
  - condition: numeric_state
    entity_id: sensor.hotwaterfull
    below: 40
action:
  - type: toggle
    device_id: 68b758a83f130fc9a62c32e75bc7c1c5
    entity_id: d8abcb7886e3cfb867e12e392b3ec168
    domain: switch
mode: single

hi

can anyone help me understand why this condition isn’t working ?

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:
image
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:

...|replace('"','')

or alternatively:

...|select('in','-.0123456789')|join

to the template.

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,


mqtt:
  sensor:
    - name: "HotWaterFull"
      state_topic: "/IOT/HotWaterPercent"
      value_template: “{{ value_json.Value | int }}”

type or paste code here

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.