Single and double quotes

Hi,

I’m trying to clean my code a little and trying to figure out when to use single and double quotes.
When I finally thought I understood I saw this two examples:

automation:
  alias: "Exterior Lighting on when dark outside"
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state_attr('sun.sun', 'elevation') }}"
    # Can be a positive or negative number
    below: -4.0
  action:
    service: switch.turn_on
    entity_id: switch.exterior_lighting

In the above example we open and close with double quotes and use single quotes inside.
This is what I believed was the correct way.

Then I checked the documentation for condtions:

condition:
  condition: and  # 'twilight' condition: dusk and dawn, in typical locations
  conditions:
    - condition: template
      value_template: '{{ state_attr("sun.sun", "elevation") < 0 }}'
    - condition: template
      value_template: '{{ state_attr("sun.sun", "elevation") > -6 }}'

In this example, we open with single quotes and use double quotes inside?!?
What did I miss? Is there a reason for this since we evaluate the elevation already inside the template?
Or is the condition documentation wrong?
Both seems to work but I would like to understand and use the correct way.

Thanks and best regards :smiley:

Makes no odds which way round, but being consistent means fewer mistakes will be made. The convention is to use double quotes outside and single inside. There is a ‘rule’ for pull requests to the documentation that they must follow the convention to try and improve consistency throughout, but occasionally a bad one slips through, or is still around from before the rule was incepted.

1 Like

The short answer is that it doesn’t matter.

The longer answer is that it doesn’t matter, but the documentation standards say " outside and ' inside. Of course, many parts of the documentation pre-date that, so they’re not updated to reflect this.

1 Like

here’s even more detail: https://developers.home-assistant.io/docs/documenting/standards#single-vs-double-quotation-marks

also, there are many places we don’t even need quotes at all. In the given examples above, you don’t need quotes for the Alias. Only if reserved characters are used, like a ‘%’ or a ‘:’ We don’t need quotes either in the states fields for triggers eg. Except when booleans are needed:

    trigger:
      platform: state
      entity_id:
        - device_tracker.ping_iphone
        - device_tracker.iphone_wifi
      to: home

or as condition:

      - condition: state
        entity_id: group.family
        state: not_home

you do need them using the boolean states:

      - condition: state
        entity_id: binary_sensor.family_home
        state: 'off'

the one way to rule them all (use that phrase for my most used condition, see below…) in my preference is preventing the need to use both (inside and outside quotes) as much as possible. With templates one can easily do that using multi-line notation:

      - condition: template
        value_template: >
          {{trigger.to_state is not none and
            trigger.from_state is not none and
            trigger.to_state.state != trigger.from_state.state}}

and an example with inner quotes only:

      - condition: template
        value_template: >
          {{not is_state('input_select.activity','Slapen')}}

the documentation examples would be like this in my setup:

automation:
  alias: Exterior Lighting on when dark outside
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: >
      {{state_attr('sun.sun','elevation')}}

and

condition:
    - condition: template
      value_template: >
        {{state_attr('sun.sun','elevation') < 0}}
    - condition: template
      value_template: >
        {{state_attr('sun.sun','elevation') > -6}}

In the above, note the superfluous configuration for the And in the conditions. Listed conditions are And’ed by default. Only when in need of ‘Or’ or the new ‘Not’ do you need to organize them like that.

I also hate unnecessary empty spaces, so I always take these out in the templates too. Thats personal preference and by No means according to any guideline (maybe even on the contrary). My eyes simply like that better and see errors quicker this way.

1 Like

Thanks all, I read all above linked documentation and that’s what made me to think that I finally understood.
But then the condition documentation confused me and I just had to check.

So to sum this up, the condition documentation is “outdated” and double quotes outside and single inside is the way to rule them all? At least to follow the standard? Right? :thinking:

1 Like

Your edit just killed my brain :wink:

sorry… tried to edit some more to kick it back to life?

Yes, it´s alive :smiley:
But I hate just knowing how, I always need to know why.
So is there some documentation where I can understand more why we need quotes for boolean states but not for others? Or do you mind explaining in some detail?

Thanks

quoting booleans (anything really) makes it a string, and not a boolean value. You need the string here. there is official documentation on that, but must find it for you.

here it is: https://www.home-assistant.io/docs/configuration/yaml/#booleans

1 Like

Right.

/me defies the overwhelming urge to break into REM’s “It’s the end of the world…”

1 Like

Thanks for pointing this out. I have submitted a PR to rectify it. Update sun elevation example to Home Assistant Documentation standards by tomlut · Pull Request #14284 · home-assistant/home-assistant.io · GitHub

1 Like

Here is something which I do not understand

I have the following call to rest_command service

call_test:
  sequence:
  - service: rest_command.pushcut_smart_notify
    data:
      notification: 'Spa_Presence'
#     payload: "{'text': 'This is a test rest_command.','title': 'Test rest'}"
      payload: '{"text": "This is a test rest_command.","title": "Test rest"}'

The line which is commented does not work and the other works. The only difference between them is the inversion of quote and double quote.
Does someone have an explanation ?

My real issue is that the expression which does not work is the one returned by a python script and I don’t know how to change it the other way round ?

notification = data.get("notification")
pdata = {}
pdata["text"] = data.get("message")
pdata["title"] = data.get("title")
service_data = {"notification": notification, "payload": pdata}
hass.services.call("rest_command", "pushcut_smart_notify", service_data, False)

Update: I add a conversion and change to the followings and now it is OK. Strange anyway…

payload = str(pdata).replace("\'", '\"')
service_data = {"notification": notification, "payload": payload}
hass.services.call("rest_command", "pushcut_smart_notify", service_data, False)