Struggling with sunrise/set and templating

Simple automation, except not… the latest in chicken coop comfort. Chickens leave the coop if it’s light out.

Outcome desired: turn on a heatlamp only when it’s dark and it’s cold. Otherwise keep it off.

If sun has set AND sun has not risen ' it's dark out AND
{{ state_attr('weather.my_house', 'temperature') | int<38}} ' it's cold
Then
Turn on switch.heatlamp
Else
Turn off switch.heatlamp

I’ve been all over the place trying to simplify and make it work. Multiple automations that check temp, sun, scripts, etc. The template evaluates true when it’s cold so that works. Right now I have an automation run every 60 min to test temp, then run a script to turn on the heatlamp for 59 min and then off. Then repeat. Uggg.

- id: '1570151248258'
  alias: chicken heater control > 38 deg < Warmer at night
  trigger:
  - minutes: /60
    platform: time_pattern
  condition:
  - condition: template
    value_template: '{{ state_attr(''weather.my_house'', ''temperature'') | int<38}}'
  action:
  - service: script.1574955861863

'1574955861863':
  alias: Toggle heater light on 59 min
  sequence:
  - data:
      entity_id: switch.022000095ccf7f6a4e91
    service: switch.turn_on
  - delay: 00:59:00
  - alias: ''
    data:
      entity_id: switch.022000095ccf7f6a4e91
    service: switch.turn_off

Any insights? Looking to learn here!

Jeff

Use your temperature sensor as a trigger without a state.

Put between sunrise, sunset, and below temp as conditions.

Put your script in the action section of the automation.

Edit: yeah this would only be to turn it on. See Tinkerers post. Much better.

My thoughts would be two automations:

- id: '1570151248258'
  alias: chicken heater control below 38 at night
  trigger:
    - platform: numeric_state
      entity_id: weather.my_house
      below: 38
      value_template: "{{ state.attributes.temperature }}"
    - platform: state
      entity_id: sun.sun
      to: 'below_horizon'
  condition:
    - condition: numeric_state
      entity_id: weather.my_house
      below: 38
      value_template: "{{ state_attr('weather.my_house','temperature') }}"
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
  action:
    - service: service: switch.turn_on
      entity_id: switch.022000095ccf7f6a4e91

- id: '1570151248258'
  alias: chicken heater control off
  trigger:
    - platform: numeric_state
      entity_id: weather.my_house
      above: 38
      value_template: "{{ state.attributes.temperature }}"
    - platform: state
      entity_id: sun.sun
      to: 'above_horizon'
  condition:
    - condition: or
      conditions:
      - condition: numeric_state
        entity_id: weather.my_house
        above: 38
        value_template: "{{ state_attr('weather.my_house','temperature') }}"
      - condition: state
        entity_id: sun.sun
        state: 'above_horizon'
  action:
    - service: service: switch.turn_off
      entity_id: switch.022000095ccf7f6a4e91

The first will turn it on when it gets cold at night, the second will turn it off when it warms up, or when the sun rises.

I think this is pretty optimal for a beginner to live with, use and adapt (for other purposes) .
You could make it smaller with templates but seriously, what would be the point ?
I’d vote for tink !

(edit - there was another chicken thread where the guy wanted 15 hours a day of light to maximise his egg production, that eventually came down to a very simple lighting automation)

It’s kind of complicated but I think this will work…I think:

  alias: chicken heater control
  trigger:
    - platform: state
      entity_id: weather.my_house
    - platform: state
      entity_id: sun.sun
  condition:
  conditions: or
    - condition: template
      value_template: "{{ is_state('switch.022000095ccf7f6a4e91', 'off') and is_state('sun.sun', 'below_horizon') and state_attr('weather.my_house', 'temperature') | int < 38 }}"
    - condition: template
      value_template: "{{ is_state('switch.022000095ccf7f6a4e91', 'on') and (is_state('sun.sun', 'above_horizon') or state_attr('weather.my_house', 'temperature') | int > 38) }}"
  action:
  - service_template:
      {% if is_state('sun.sun', 'below_horizon') and state_attr('weather.my_house', 'temperature') | int < 38 %}
        switch.turn_on
      {% else %}
        switch.turn_off
      {% endif %}
    entity_id: switch.022000095ccf7f6a4e91

I’m pretty sure this is the same guy…:laughing:

Yes, Same guy! I’ll give that a try. Looks logical and reasonable.

Jeff

1 Like

It doesn’t like this:

condition:
conditions: or
- condition: template <- says indenting wrong on this line. But no indenting I try seems to work.

oops.

try this:

condition:
  condition: or
  conditions:
    - condition:...

way too many indents and “condition:”'s :wink:

Agreed. Still no love. I’ll try the two automation route next.

Don’t give up quite so easily…:wink:

I also realized that I forgot to put in the multi-line template indicator.

I just put the following into my config and the config-check came back as valid so try this if you want:

  - alias: chicken heater control
    trigger:
      - platform: state
        entity_id: weather.my_house
      - platform: state
        entity_id: sun.sun
    condition:
      condition: or
      conditions:
        - condition: template
          value_template: "{{ is_state('switch.022000095ccf7f6a4e91', 'off') and is_state('sun.sun', 'below_horizon') and state_attr('weather.my_house', 'temperature') | int < 38 }}"
        - condition: template
          value_template: "{{ is_state('switch.022000095ccf7f6a4e91', 'on') and (is_state('sun.sun', 'above_horizon') or state_attr('weather.my_house', 'temperature') | int > 38) }}"
    action:
      - service_template: >
          {% if is_state('sun.sun', 'below_horizon') and state_attr('weather.my_house', 'temperature') | int < 38 %}
            switch.turn_on
          {% else %}
            switch.turn_off
          {% endif %}
        entity_id: switch.022000095ccf7f6a4e91

That’s what I get for writing it directly in the forum post without running it through my config checker first.

Again, sorry about that.

Brilliant. Fixed the indents and it works. No way I could have come up with that myself.

Much appreciated.