First automation, need a little help please

Being new to this and coming over from the MagicMirror2 project I’m trying to learn to write my own automations.

I have searched through the forums and have found bits and pieces and they are great but I still cannot get my automation to work for some reason. Here is two examples of what I’ve tried. Can anyone please give me some insight to why they aren’t working, I appreciate the input!! :slight_smile:

- alias: '13 - Turn on Light[s] on at sunset'
  id: Turn on Light[s] on at sunset 
  trigger:
    - platform: state
      entity_id: device_tracker.john_s_galaxy_note20_ultra_5g
      from: 'not_home'
      to: 'home'
  action:
    service_template: light.turn_{{ 'on' if is_state('sun.sun', 'below_horizon') else 'off' }}
    entity_id: light.livingroom1

2nd way:

#- alias: '13 - Turn on light[s] on sunset'
#  id: Turn on light[s] on sunset
#  trigger:
#  - platform: state
#    entity_id: sun.sun
#    from: below_horizon
#    to: above_horizon
#  condition:
#    condition: state
#    entity_id: device_tracker.john_s_galaxy_note20_ultra_5g
#    state: 'home' 
#  action:
#    service: light.turn_on
#    entity_id: light.livingroom1

You can’t do service templates like that as far as I know. Do this instead,

    - service_template: >
        {% if is_state('sun.sun', 'below_horizon') %}
        light.turn_on
        {% else %}
        light.turn_off
        {% endif %}

Thank you! I’ll give that a try and I guess I’ll find out later today! :slight_smile:

So here goes :slight_smile:

- alias: '13 - Turn on Light[s] on at sunset'
  id: Turn on Light[s] on at sunset 
  trigger:
    - platform: state
      entity_id: device_tracker.john_s_galaxy_note20_ultra_5g
      from: 'not_home'
      to: 'home'
  action:
    - service_template: >
        {% if is_state('sun.sun', 'below_horizon') %}
        light.turn_on
        {% else %}
        light.turn_off
        {% endif %}
      entity_id: light.livingroom1

You can. That’s an inline if statement and I use it frequently.

1 Like

Your automation should work fine. What’s not working about the automation? Also, as @123 said, inline if statements work just fine. So there is no difference between your code and @callifo’s.

My guess is that you have a minor typo in one of the entity_id names or sun.sun isn’t integrated into the system.

I stand to be corrected, I have never seen that before and it looked quite strange.

No problem. An inline if reads like this:

It's this, if something is true, else it's that

Example:

{{ 'Hello' if states('input_boolean.home') == 'on' else 'Goodbye' }}

I am going to go with perhaps sun.sun isn’t integrated as it didn’t fire and by all account it should have :slight_smile: I’ll check that integration :slight_smile:
Thank you!

This what I came up with but it’s not turning on the light… but sensor.elevation is showing for example 7.5 ° I’m thinking the degree symbol may be the problem. Not really sure I’ve tried the above 2 and that makes 3 automations and none of them are working…LOL…oh new guys. :slight_smile:

Just trying to lean this all.

does the degree symbol show up in the state on the devtools -> states page?

No it doesn’t :slight_smile: I can’t wait until this all becomes easy for me.

sorry if you’ve already checked this but on the logbook page does it even show your automation being fired up when you phone goes to “home”?

‘logbook’ page? Do you means logs? Yeah I’m really new to all this … learning as fast as I can :slight_smile:

Ok, then the degree symbol is not screwing it up and the system see’s it as a number.

The only way this will trigger is if device_tracker.sm_n986u1 changes from not_home to home and the elevation is less than (not equal to) 1.

Yes sir. In the past 3 nights when both conditions are met, still not firing. So perhaps a typo?

Yes its the entry called “Logbook” in the sidebar of the main overview screen in the web interface… very handy for seeing if your trigger is even working

Oh ok WOW… Thank you! That’s VERY helpful!