var: covid_daily: friendly_name: "Daily Covid count" initial_value: 0 icon: mdi:virus-outline covid_yesterday_total: friendly_name: "Yesterdays NZ Cumulative Total Covid Cases" initial_value: 0 icon: mdi:virus-outline automation: - id: add_covid_confirmed_stats_only_if_increases alias: add_covid_confirmed_stats_only_if_increases #description: # "Check new state of sensor.new_zealand_coronavirus_confirmed and store it # in input_number.new_zealand_coronavirus_confirmed_previous_value if it has increased" trigger: - platform: state entity_id: sensor.new_zealand_coronavirus_confirmed condition: - condition: template value_template: "{{ (states('sensor.new_zealand_coronavirus_confirmed') | float > states('var.covid_yesterday_total') | float) }}" action: - service: var.set data_template: entity_id: var.covid_daily value: "{{ states('sensor.new_zealand_coronavirus_confirmed') | float - states('var.covid_yesterday_total') | float }}" - service: var.set data_template: entity_id: var.covid_yesterday_total value: "{{ states('sensor.new_zealand_coronavirus_confirmed') | float }}" sensor: - platform: statistics entity_id: sensor.coronavirus_totals_normalised sampling_size: 200000 state_characteristic: mean max_age: days: 1 name: "Coronavirus Statistics" - platform: template sensors: coronavirus_totals_normalised: friendly_name: "NZ Coronavirus Totals (Normalised)" value_template: > {% set new_state = states('sensor.new_zealand_coronavirus_confirmed') | float %} {% if new_state >= states('input_number.new_zealand_coronavirus_confirmed_previous_value') | float %} {{ new_state }} {% else %} {{ states('input_number.new_zealand_coronavirus_confirmed_previous_value') | float }} {% endif %}