Syntax for sun handling?

Hi

I’m trying to enhance my automation that I first had based on sunset but I need to tweak it more so it activates a little while before sunset so I did that:

- alias: Lecture PMP
  trigger:
    - platform: state
      entity_id: media_player.pmp_salon
      to: 'playing'
      from: 'idle'
  condition:
    - condition: state
      entity_id: sun.sun
      value_template: '{{ state.attributes.elevation }}'
      below: 7
  action:
    service: scene.turn_on
    entity_id: scene.Salon_cinema

But it refuses my state Elevation and value I pass to it. Tried to find some samples of code but not much success on this :frowning:

Vincèn

Here is my automation that triggers a little while before sunset:

trigger:
    platform: sun
    event: sunset
    offset: "-00:15:00"
  action:

I bet you need to cast it as an integer.

  condition:
    - condition: state
      entity_id: sun.sun
      value_template: '{{ state.attributes.elevation | int }}'
      below: 7

I use elevation and cloudiness to create a sensor that I use for light automation.


  - platform: template
    sensors:
      turn_on_indoor_lights:
        friendly_name: 'Turn On Indoor Lights'
        value_template: >
          {% if (states.sun.sun.attributes.elevation | int < 30) %}true
          {% elif ( (states.sun.sun.attributes.elevation | int < 40) and (states.sensor.dark_sky_cloud_coverage.state | int > 50)) %}true
          {% elif (states.sensor.dark_sky_cloud_coverage.state | int > 90) %}true
          {% else %}false
          {% endif %}
1 Like

@treno thanks but it’s condition I need, not for a trigger :wink:
@ih8gates very good idea to go through a variable (sensor in HA terminology) as it can be reused for other automations. I still have an issue to implement it as I added your logic at my configuration file:

sensor:
  - platform: template
    sensors:
      nuit:
        friendly_name: 'Mode Nuit'
        value_template: >
          {% if (states.sun.sun.attributes.elevation | int < 20) %}true
          {% elif ( (states.sun.sun.attributes.elevation | int < 30) and (states.sensor.dark_sky_cloud_coverage.state | int > 40)) %}true
          {% elif (states.sensor.dark_sky_cloud_coverage.state | int > 80) %}true
          {% else %}false
          {% endif %}

Is it normal that the variable has unknown state in States tab ?? Isn’t it evaluated at startup of HA ?
I then modified my automation with that code now:

- alias: Lecture PMP
  trigger:
    - platform: state
      entity_id: media_player.pmp_salon
      to: 'playing'
      from: 'idle'
  condition:
    - condition: state
      entity_id: sensor.nuit
      state: true
  action:
    service: scene.turn_on
    entity_id: scene.Salon_cinema

but it complains about:

2017-07-10 16:50:34 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: extra keys not allowed @ data[‘condition’][0][‘state’]. Got None
not a valid value for dictionary value @ data[‘condition’][0][‘condition’]. Got None. (See /home/homeassistant/.homeassistant/configuration.yaml, line 219). Please check the docs at Automation - Home Assistant
so a little lost here for now :frowning:

Do you need quotes around true in the condition’s state?