Got stuck with automation

Hi
I got stuck with automation and can’t find out how to move on.
My configuration is simple. I have a ESPHome device with temperature sensor and relay with heater. And I want HA turn off or turn on heating when temperature goes above/below threshold. Logically is simple :slight_smile:

ESPHome device sends every minute data to HA

[10:39:25][D][api.connection:604]: Client 'Home Assistant 2021.3.4 (192.168.88.2)' connected successfully!
[10:39:25][D][api.connection:604]: Client 'Home Assistant 2021.3.4 (192.168.88.2)' connected successfully!
[10:40:03][D][dht:048]: Got Temperature=20.9°C Humidity=93.0%
[10:40:03][D][sensor:092]: 'Snail House Temperature': Sending state 20.90000 °C with 1 decimals of accuracy
[10:40:03][D][sensor:092]: 'Snail House Humidity': Sending state 93.00000 % with 1 decimals of accuracy
[10:41:03][D][dht:048]: Got Temperature=20.7°C Humidity=93.0%

I know that every minute is too often but it is for debug purposes. And HA sees sensor and relay, I can turn on/off relay and can read temperature from dashboard.

And this is my automation:

- id: '1616958574282'
  alias: Snail House
  description: Automation for snail house heater
  trigger:
  - platform: numeric_state
    entity_id: sensor.snail_house_temperature
    value_template: '{{ state.attribute.temperature }}'
    attribute: attribute_name
    for: 0:10:00
    above: '20'
  - platform: numeric_state
    entity_id: sensor.snail_house_temperature
    attribute: friendly_name
    for: 0:10:00
    below: '19'
    value_template: '{{ state.attribute.temperature }}'
  condition: []
  action:
  - choose:
    - conditions:
      - condition: numeric_state
        entity_id: sensor.snail_house_temperature
        below: '19'
        value_template: '{{ state.attribute.temperature }}'
      sequence:
      - service: script.snail_house_turn_on_heater_script
      - service: notify.persistent_notification
        data:
          data:
            message: message - heating is ON
            title: title - heating is ON
    - conditions:
      - condition: numeric_state
        entity_id: sensor.snail_house_temperature
        above: '20'
"homeassistant/config-ha/automations.yaml" 51L, 1535C                                                                                                                         9,13          Top
    for: 0:10:00
  action:
  - choose:
    - conditions:
      - condition: numeric_state
        entity_id: sensor.snail_house_temperature
        below: '19'
        value_template: '{{ state.attribute.temperature }}'
      sequence:
      - service: script.snail_house_turn_on_heater_script
      - service: notify.persistent_notification
        data:
          data:
            message: message - heating is ON
            title: title - heating is ON
    - conditions:
      - condition: numeric_state
        entity_id: sensor.snail_house_temperature
        above: '20'
        value_template: '{{ state.attribute.temperature }}'
      sequence:
      - service: script.snail_house_turn_off_heater
      - service: notify.persistent_notification
        data:
          data:
            message: message - heating is off
            title: title - heating is OFF
    default:
    - service: notify.persistent_notification
      data:
        data:
          message: Temperature is ok
          title: TEMP IS OK
  mode: queued
  max: 10

again - persistent notifications are just for debug purposes.

and this doesn’t work. I would appreciate it if anyone would point/advise what I am doing wrong

Can you show us the entity in developer tools: sensor.snail_house_temperature


I don’t know how to find an entity in a yaml format so I attached a screenshot

Create a generic thermostat using that sensor

1 Like

Go to developer tools and type in the sensor name at the entity line.

Screenshot 2021-03-30 at 11.00.13
this is what I have

could you please elaborate?

You dont need this line in trigger. please try trigger without it like below.

  trigger:
  - platform: numeric_state
    entity_id: sensor.snail_house_temperature
    for: '00:10:00'
    above: '20'
  - platform: numeric_state
    entity_id: sensor.snail_house_temperature
    for: '0010:00'
    below: '19'

This function is built into ha


No need to reinvent the wheel
1 Like

Then it was as I thought. The attribute lines are wrong. They should not be there.
You only have state in the entity to care about.
But I agree with thermostat.

Remove attributes lines too. They shouldn´t be there I believe

1 Like

The benefit of using a thermostat over the automation is massive.
Home / away, preset modes, easy to adjust and see what’s happening in the UI, etc

I tried and I got errors in logs

Logger: homeassistant.components.homeassistant.triggers.numeric_state
Source: components/homeassistant/triggers/numeric_state.py:161
Integration: Home Assistant Core Integration (documentation, issues)
First occurred: March 29, 2021, 9:21:25 PM (158 occurrences)
Last logged: 11:49:03 AM

    Error in 'Snail House' trigger: In 'numeric_state' condition: entity sensor.snail_house_temperature state '°C' cannot be processed as a number
    Error in 'Snail House' trigger: In 'numeric_state' condition: entity sensor.snail_house_temperature state 'Snail House Temperature' cannot be processed as a number
    Error in 'Snail House' trigger: In 'numeric_state' condition: attribute 'attribute_name' (of entity sensor.snail_house_temperature) does not exist
    Error in 'Snail House' trigger: In 'numeric_state' condition: entity sensor.snail_house_temperature state '' cannot be processed as a number
    Error in 'Snail House' trigger: In 'numeric_state' condition: template error: UndefinedError: 'homeassistant.core.State object' has no attribute 'attribute'

currently automation looks like

- id: '1616958574282'
  alias: Snail House
  description: Automation for snail house heater
  trigger:
  - platform: numeric_state
    entity_id: sensor.snail_house_temperature
    for: 0:01:00
    above: '20'
  - platform: numeric_state
    entity_id: sensor.snail_house_temperature
    for: 0:01:00
    below: '19'
  condition: []
  action:
  - choose:
    - conditions:
      - condition: numeric_state
        entity_id: sensor.snail_house_temperature
        below: '19'
      sequence:
      - service: script.snail_house_turn_on_heater_script
      - service: notify.persistent_notification
        data:
          data:
            message: message - heating is ON
            title: title - heating is ON
    - conditions:
      - condition: numeric_state
        entity_id: sensor.snail_house_temperature
        above: '20'
      sequence:
      - service: script.snail_house_turn_off_heater
      - service: notify.persistent_notification
        data:
          data:
            message: message - heating is off
            title: title - heating is OFF
    default:
    - service: notify.persistent_notification
      data:
        data:
          message: Temperature is ok
          title: TEMP IS OK
  mode: queued
  max: 10

I will try to use thermostat later, first I’d like to make this approach work to learn how to not make mistakes in a future :slight_smile:

You haven’t reloaded the automations. That error refers to an earlier state of your code, when you’d included attribute lines for unknown reasons.

The docs also suggest that the for: time should be quoted although that may not be necessary.

Also, listen to @samnewman86 and set this up as a generic thermostat.

You should check out generic thermostat. But if you want the automation, you can try this.

alias: Snail House
description: ''
mode: single
trigger:
  - platform: state
    entity_id: sensor.snail_house_temperature
    for: '00:01:00'
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: '{{states(''sensor.snail_house_temperature'')|int > 20 }}'
        sequence:
          - service: script.snail_house_turn_off_heater
          - service: notify.persistent_notification
            data:
              message: message - heating is OFF
              title: title - heating is OFF
      - conditions:
          - condition: template
            value_template: '{{states(''sensor.snail_house_temperature'')|int < 19 }}'
        sequence:
          - service: script.snail_house_turn_on_heater
          - service: persistent_notification.create
            data:
              message: message - heating is on
              title: title - heating is ON
    default:
      - service: persistent_notification.create
        data:
          message: Temperature is ok
          title: TEMP IS OK

Please copy paste the automation yaml like below

1 Like

it works!!! Thank you very much!!! I am happy now.
Will enjoy a day with this automation and will start trying Generic Thermostat :slight_smile:

1 Like

@sheminasalam, may I ask you to explain me one confusing thing?
my ESP device sends data every 10 minutes. So for 9 hours I’d expect to have 9(hours)*6(message per hour) = 54 message. However in my notification area I have only 17, They are coming 2-3 per hour. And I see in ESPHome logs that messages are being sent quite a lot. What could be a reason?

It sends the data but if there is no change then HA makes no note of it.
You can enable it to make note of all packets, but you have no use of it.

1 Like

got it. It makes sense - there barely would be a point to process the same message.
But how can I enable it? Just for sake of curiosity?

I can’t remember what it’s called