Sensor based on Day of Month Number

Hi, I am trying to set a value based on the day number. If it is the 1st of each month I would like the sensor to default to 100. Else I would like the value of the sensor to default to something else (this value is calculated based on the predicted solar and works fine)

For some reason I cannot carry out the check on if it is the first day of the month or not. I am sure its simple but alas I am stuck!

Cheers

sensors:
  day_number: '{{now().strftime("%-d")}}'

trigger:
  value_template: >-
    {% if states('day_number') < '1' %}
      do_this
    {% else %} 
      do_something_else
    {% endif %}

The day number can’t be less than one.
And day_number is a sensor so it should be sensor.day_number.

sensors:
  day_number: '{{now().strftime("%-d")}}'

trigger:
  value_template: >-
    {% if states('sensor.day_number') == '1' %}
      do_this
    {% else %} 
      do_something_else
    {% endif %}

ahh, good spot :slight_smile:

I have tweaked that as per your code, but it keeps jumping to the ‘do_something_else’ output (I am setting the comparison to ‘8’ as its the 8th today

Interestingly if I change the value it makes no difference, but if I change it to greater than 8 it then goes to ‘do_this’. But then when changing the value it remains on the ‘do_this’ output

I think its close,

cheers

What does the state of the sensor say it is?
Or is it even a sensor you created?

Ignore the month part, that is for something else :slight_smile:

Do you have a sensor called sensor.day_number?
If you are just testing in template tools then you need a different formatting.

I am just testing now before I commit it to my config. I can try adding to config if you think that will sort it? cheers

OK, I have added it to my conf.yaml

  - platform: template
    sensors:
      day_number: 
        friendly_name: Day of the Month
        value_template: >-
          '{{now().strftime("%-d")}}'

  - platform: template
    sensors:
      target_soc:
        friendly_name: Target SOC for Night Charge
        unit_of_measurement: "%"
        value_template: >-
          {% if states('sensor.day_number') == '8' %}
          100
          {% else %}
          {% set state = 2.5 * states('sensor.solcast_forecast_today') | float(0) %}
          {% set state = 150 - state | float(0) %}
          {% set y = (((state/5) | int) +1) * 5 %}
          {{ ([35, y, 95] | sort)[1] }}
          {% endif %}

I have set the day to ‘8’ in the if statement. This should evaluate the ‘target_SOR’ to 100%

but it is still going to 95%

I have checked and the ‘sensor.day_number’ is evaluating to ‘8’

Cheers

- platform: template
    sensors:
      day_number: 
        friendly_name: Day of the Month
        value_template: '{{ now().day }}'

      target_soc:
        friendly_name: Target SOC for Night Charge
        unit_of_measurement: "%"
        value_template: >-
          {% if states('sensor.day_number') == '8' %}
          100
          {% else %}
          {% set state = 2.5 * states('sensor.solcast_forecast_today') | float(0) %}
          {% set state = 150 - state | float(0) %}
          {% set y = (((state/5) | int(0)) +1) * 5 %}
          {{ ([35, y, 95] | sort)[1] }}
          {% endif %}

NOTE

Although unrelated to the problems you have encountered, you should consider using modern format to configure Template Sensors. You’re using legacy format which, although still accepted, won’t receive any enhancements in the future.

And my teensie 2cts… why have a separate sensor for daynumber? Are you using this elsewhere ? Just thinking about unnecessary clutter

this seems to work :slight_smile:

What was wrong with my previous line to get the day on the month? Hopefully I can then not make the same error

cheers

I dont currently use the day in the month on anything else, but I plan to so I decided to strip it out.

When the template is on the same line as the option, the template must be wrapped in quotes.

Reference: Important Templating Rules

However, if you use a line-continuation character, like > or |, to indicate that the template begins on the next line, the template should not be wrapped in quotes. If you do wrap it in quotes, the quotes will become part of the result produced by the template.

AHH, dint know that.many thanks

1 Like