Finding max value from an MQTT sensor

Hi,

How would I create a template sensor that shows the maximum value from an MQTT sensor?

I’ve tried the following, but see an error relating to a python dict item not being available:

sensor:

  • platform: mqtt
    state_topic: “/house/nursery/temp”
    unit_of_measurement: “C”
    name: “Nursery temperature”
  • platform: template
    sensors:
    max_nursery_temperature:
    friendly_name: “Maximum temperature”
    value_template: {{ max(states.nursery_temperature) }}
    unit_of_measurement: “C”

I use some sensor templates…

- platform: template
  sensors:
      greenmaxtemp:
          friendly_name: 'greenhouse max temp'
          value_template: >
              {% set greencurrenttemp1 = (states.sensor.gh_sensor_temp.state | float) %}
              {% set greencurrent_threshold_max = (states.input_slider.green_max_temp.state | float) %}
              {% macro max(X, Y) -%} {{X|float if X|float > Y|float else Y|float }} {%- endmacro %}
              {{ max(greencurrent_threshold_max, greencurrenttemp1) }}

- platform: template
  sensors:
      greenmintemp:
          friendly_name: 'greenhouse min temp'
          value_template: >
              {% set greencurrenttemp2 = (states.sensor.gh_sensor_temp.state | float)%}
              {% set greencurrent_threshold_min = (states.input_slider.green_min_temp.state | float) %}
              {% macro min(X, Y) -%} {{X|float if X|float < Y|float else Y|float }} {%- endmacro %}
              {{ min(greencurrent_threshold_min, greencurrenttemp2) }}

And some input sliders…

green_max_temp:
  name: Greenhouse Max Temp
  initial: -40
  min: -40
  max: 50
  step: 0.01
green_min_temp:
  name: Greenhouse Min Temp
  initial: 50
  min: -40
  max: 50
  step: 0.01

Thanks Keith,

Am I right in saying that yours compares two different values to find which is the highest and which of a pair is the smallest?

I’m trying to look through the history of all values from my nursery sensor and then display this.

Matt

Have you looked at …

or

Mine stores the lowest and highest values in the input sliders and compares against the read ones from the sensors. I store the values to disc so that they permanent immediately after they have changed.

Thank you. I’ve tried the statistics sensor, but it doesn’t seem to read data from the history.

I’ll try the history statistics sensor to see how I get on with that. I’d then like to reference the maximum value only so that I can get this returned by an Alexa skill.

Matt